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
Hello

In some testing scenarios I'd like to override the behavior of WhenCalled().WillReturn() for the same function.
However, it seems like only the first instruction takes part, instead of being overridden by a later one.

Consider the following example:

[TestMethod]
public void CreateUsersExceptionTest()
{
Isolate.WhenCalled(() => MyObj.MyStaticMethod()).WillReturn(0);
Isolate.WhenCalled(() => MyObj.MyStaticMethod()).WillReturn(1);

Assert.AreEqual(1, MyObj.MyStaticMethod());
}

This tests fails with "Assert.AreEqual failed. Expected:<1>. Actual:<0>."

1. Is that the expected behavior of multiple WhenCalled().WillReturn() instructions?
2. If I do want to override the behavior, what's the best workaround?
If possible, I'd like to avoid calling Isolate.CleanUp().

Thanks
Yaakov
asked by yaakovb (600 points)

3 Answers

0 votes
Hello,

What you described is called Sequenced method calls.

When you set a behavior on a method call, it is valid for all the calls to that method from that point and on.

However, if you set multiple behaviors on the same method, you create a sequence in which the first behavior set is valid to the first call, the second one is valid to the second call, and so forth.
The last behavior set is valid to the n-th call and all calls from that point and on (n is the number of the behaviors that you set on the method).

For instance, in your example, the first call to MyObj.MyStaticMethod() will return 0, and the second call to MyObj.MyStaticMethod() will return 1.
Any addition call to MyObj.MyStaticMethod() will return 1, for it is the last behaior that was set.

You can find more information in Isolator documentation
answered by NofarC (4k points)
0 votes
While it sounds like a feature of Typemock, this "sequence method calling" makes WhenCalled.WillReturn unusable for me. What is alternative to this? Any other function I can use instead?
answered by ming.liu (140 points)
0 votes
Hi,

Can you please send me an use-case example where WhenCalled.WillReturn is unusable?
It will help if you add your code for demonstration.
answered by alex (17k points)
...