Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Fake.AllInstances<MyType>() not behaving as described

0 votes

I was having some trouble using Fake.AllInstances<> so I began simplifying my code and it is now as simple as it can get and it still seems to be behaving differently from what I have seen described here in the answers to other questions.

My understanding is that when I call Isolate.Fake.AllInstances<TradeSummary>(), all existing and future instances of TradeSummary will be the same as the fake returned from this call.  Furthermore, I can use the returned fake to set up WhenCalled() to perform subsequent verification of method calls.

Here is my code:

			TradeSummary fakeTradeSummary = Isolate.Fake.AllInstances<TradeSummary>();
			Isolate.WhenCalled(() => fakeTradeSummary.Summarize(null)).CallOriginal();
			TradeSummary summ = new TradeSummary();
			summ.Summarize(new List<Trade>());
			Assert.AreSame(fakeTradeSummary, summ);
			Isolate.Verify.WasCalledWithAnyArguments(() => summ.Summarize(null));
This code fails in 2 ways that seem counter to what I am expecting.
First, the Assert.AreSame() fails because the 2 objects are not the same instance.
Second, the Verify.WasCalledWithAnyArguments() fails with the following exception:
TypeMock.TypeMockException : *** Isolate.Verify does not support objects that were not faked
 using Isolate.Fake.Instance(), or passed through WhenCalled()

Am I doing anything incorrectly in this code?

 

asked Nov 26 by KentRollins (380 points)

1 Answer

+2 votes
 
Best answer

Hey Kent,

Your understanding of Isolate.Fake.AllInstances() is almost correct: it creates a handle, on which you can set the desired behavior. Everytime you instantiate a fakeTradeSummary object, it will point to the same handle.

About your code, summ was indeed faked and pointed to fakeTradeSummary. 

However, Isolate.Verify.WasCalledWithAnyArguments() fails because you used WhenCalled() on fakeTradeSummary.

If you use it on summ, it won't fail.

Assert.AreSame verifies that the specified objects refer to the exact same object. 

Since fakeTradeSummary and summ are not the exact same object, Assert.AreSame() fails.

Notice that if you use Assert.AreNotSame(summ, fakeTradeSummary) the test will pass, because the two objects are different.

See the test below:

public void test()
{
    TradeSummary fakeTradeSummary = Isolate.Fake.AllInstances<TradeSummary>();
    TradeSummary summ = new TradeSummary();
    Isolate.WhenCalled(() => summ.Summarize(null)).CallOriginal();
    summ.Summarize(new List<Trade>());
    Isolate.Verify.WasCalledWithAnyArguments(() => summ.Summarize(null));
    Assert.AreNotSame(summ, fakeTradeSummary);
}

You can also write your test like this:

public void test()
{
    TradeSummary fakeTradeSummary = Isolate.Fake.AllInstances<TradeSummary>();
    Isolate.WhenCalled(() => fakeTradeSummary.Summarize(null)).CallOriginal();
    TradeSummary summ = new TradeSummary();
    summ.Summarize(new List<Trade>());
    Assert.AreNotSame(fakeTradeSummary, summ);
    Isolate.Verify.WasCalledWithAnyArguments(() => fakeTradeSummary.Summarize(null));
 }

Please let me know if it helps. 

answered Nov 26 by SapirTypemock (1,550 points)
edited Dec 3 by SapirTypemock

Sapir, thank you for your answer.  I think I am beginning to see my misunderstanding, but it leads to more questions.

First, I have not seen the concept of a "handle" mentioned in the documentation.  I have seen it in answers here on the forums, but I am just now understanding that this does not mean "reference" and is a concept specific to TypeMock.  I previously thought Fake.AllInstances() would cause all subsequent instantiations to return the same instance.  Is the handle concept documented anywhere?  I would certainly recommend detailing this more thoroughly in the documentation of methods such as this one.

Second, the code that I showed was greatly simplified from what I would really like to test so that I could understand what was really failing.  Here is how I would really like the test to be structured:

    // arrange
    Trader trader = new Trader();
    trader.InSampleTrades = new List<Trade>();
    TradeSummary fakeTradeSummary = Isolate.Fake.AllInstances<TradeSummary>();
    Isolate.WhenCalled(() => fakeTradeSummary.Summarize(null)).CallOriginal();
    
    // act
    trader.SummarizeTrades();
    
    // assert
    Assert.IsNotNull(trader.InSampleTradeSummary);
    Isolate.Verify.WasCalledWithAnyArguments(() => trader.InSampleTradeSummary.Summarize(null));

 

So here, trader.SummarizeTrades() is the call that internally results in the instantiation of a TradeSummary instance (assigned to trader.InSampleTradeSummary) and I want to verify that trader.InSampleTradeSummary.Summarize(any) was called as shown in the last line.  But that Verify fails indicating that call was not made when it actually was.  So I think I now understand why this isn't working (the "handle" thing).  Is there a way to verify this call?

Hey Kent, 

Thank you for your feedback! we will definitely add more information about Isolate.Fake.AllInstances() to our documentation as soon as possible. 

Let me sharpen it a little more. Isolate.Fake.AllInstances() sets the same behavior on all past and future instances of a specific type.

If a new instance is created, it will behave as described, but  if an instance is created using Fake.Instance or Fake.NextInstance it will not be affected by the behavior described at Isolate.Fake.AllInstances().

Can you send me the code of class Trader so I can understand the problem better? 

Thanks. 

 

Hey Kent, 

You can write your test like this:

public void test()
{
       // arrange
       Trader trader = new Trader();
       trader.InSampleTrades = new List<Trade>();
       TradeSummary fakeTradeSummary = Isolate.Fake.AllInstances<TradeSummary>();
       Isolate.WhenCalled(() => fakeTradeSummary.Summarize(null)).CallOriginal();

       // act
       trader.SummarizeTrades();

       // assert
       Assert.IsNotNull(trader.InSampleTradeSummary);
      Isolate.Verify.WasCalledWithAnyArguments(() => fakeTradeSummary.Summarize(null));
}

 

Hey Kent, 

Have you tried writing the test as suggested above? 

Hey Kent,

We updated the Isolate.Fake.AllInstances() section on our documentation. 

You can watch the new description here:

https://www.typemock.com/docs?book=Isolator&page=Documentation%2FHtmlDocs%2Ffakingallinstancesofatype.htm

Would you say that it's clearer now? 

...