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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I have code:

Foo mock = Isolate.Fake.Instance<Foo>();
Isolate.Swap.NextInstance<Foo>().With(mock);
Isolate.WhenCalled(() => mock.Method(null)).WillReturn(fakeResults);


Problem is the method I'm testing internal creates an instance of Fool and calls the method that we have 'WhenCalledFor', disposes Foo, does some heavy lifting, then creates an instance of Foo again to call a different method.

Problem is that the 2nd instance of Foo created is creating a 'real' Foo, which I don't want, ideally I want:

Foo mock = Isolate.Fake.Instance<Foo>();
Isolate.Swap.AllFutureInstances<Foo>().With(mock);
Isolate.WhenCalled(() => mock.Method(null)).WillReturn(fakeResults);
Isolate.WhenCalled(() => mock.SomeOtherMethod(null)).WillReturn(otherFakeResults);


Would the record/replay syntax be better for this?
asked by boo (21.8k points)

1 Answer

+1 vote
Figured it out:

Foo mock = Isolate.Fake.Instance<Foo>();
Isolate.Swap.NextInstance<Foo>().With(mock);
Isolate.WhenCalled(() => mock.Method(null)).WillReturn(fakeResults);
Isolate.Swap.NextInstance<Foo>().With(mock);
Isolate.WhenCalled(() => mock.SomeOtherMethod(null)).WillReturn(otherFakeResults);


So basically call Swap.NextInstance for each time you expect the value to be called (in my case actually 3).

Enhancement request - maybe add the SwapAllInstances method in my psuedo code above and/or SwapNextInstance(int) where the argument is the number of instance to swap.
answered by boo (21.8k points)
...