chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
In Isolator 5.4.5, this first scenario with a Swap.NextInstance() followed by a WhenCalled() with a non-fake throws an exception saying use fakes not reals.

But this second scenario with just a WhenCalled() with a non-fake does not throw an exception. If WhenCalled() should indeed be used with fake instances only then expected is an exception for this second scenario.

[ img ]

public class A
{
   public int X { get; private set; }
}

[TestMethod]
public void Test1()
{
   A aToSwap = new A();
   Isolate.Swap.NextInstance<A>().With(aToSwap);

   A nextA = new A();
   // This call to WhenCalled() with a non-fake
   // results in an exception with message 
   // "*** WhenCalled() should be used on fake instances only."
   Isolate.WhenCalled(() => nextA.X).WillReturn(1);
}

[TestMethod]
public void Test2()
{
   A a = new A();
   // This call to WhenCalled() with a non-fake
   // results in no exception.
   Isolate.WhenCalled(() => a.X).WillReturn(1);
}
asked by Neil (27.7k points)

4 Answers

0 votes
Hi Neil,

WhenCalled can be used with fake or real/live (non-fake) objects.
The reason you did recieve an error message is because WhenCalled was used on a live object that was swapped.

If you want to set expectation on the swapped object do it before instantiating it:

A aToSwap = new A();
Isolate.Swap.NextInstance<A>().With(aToSwap);
// set expectation
Isolate.WhenCalled(() => aToSwap.X).WillReturn(1);

A nextA = new A();


I will report this issue to our dev team and we will decide what is the best solution for this issue.
answered by dhelper (11.9k points)
0 votes
Thanks DHelper.
answered by Neil (27.7k points)
0 votes
Hi Neil,

Usually we do not recommend setting behavior on swapped objects.

You should either set the behavior before the object is created or don't use Isolate.Swap if you can pass the new object to WhenCalled (like your example).

Furthermore you should divide your test to three different parts:
1. Arrange - where you create your fake object, set behavior etc.
2. Act - Where you call the method under test
3. Assert - check the test condition using Asserts or Verify

Because of that Isolator does not support setting behavior on future object (swapped) by design. I will p[ass a request to change the error message.


Regards,

Dror Helper
answered by dhelper (11.9k points)
0 votes
Thanks Dror.
answered by Neil (27.7k points)
...