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
Hey guys,

i have a function which calls a helper function twice. Now i need to return different results for the second call. How can i implement this?

Thanks in advance!

Bye, Michael
asked by michaels (640 points)

11 Answers

0 votes
Hi Michaels

All you have to do is use Isolate.WhenCalled() twice and specify a different return value for each call. The first call to Isolate.WhenCalled will react to the first call of the faked function and second call of Isolate.WhenCalled will react to the second call of the faked function.

Example:
[Test]
public void Test()
{
    var fake = Isolate.Fake.Instance<Foo>();
    Isolate.WhenCalled(() => fake.Bar()).WillReturn(10);
    Isolate.WhenCalled(() => fake.Bar()).WillReturn(20);

    Assert.AreEqual(10, fake.Bar());
    Assert.AreEqual(20, fake.Bar());
}


:arrow: Note that after fake.Bar() is called for the second time 20 will become its default return value and any call to it will return 20.

Hope it helps.
answered by ohad (35.4k points)
0 votes
Thanks a lot!
answered by michaels (640 points)
0 votes
Hi,

How should I do in order to cancel(reset) one behavoir.

Example:

Isolate.WhenCalled(....).WillReturn(2);
Isolate.WhenCalled(....).WillRetun(3);

I would like that the method to return 3 all times, starting at first call.

Thank you!
answered by psimion41@yahoo.com (260 points)
0 votes
Hi,

In order to make the method return 3 always what you should do is call WhenCalled() once:
Isolate.WhenCalled(....).WillRetun(3);

Usually you want to set one behavior to a method no matter how many time it is called.
To that just use WhenCalled() once (like in the example above).

When you use WhenCalled() more than once on the same method you are creating a sequenced behavior.
Sequenced method calls are useful when you want to call the same method more than once and you want it to return a different value on every call.
For instance, in your example what would happen is:
*The first call to your method will return 2.
*From the second call and on your method will return 3.

You can find more information here

Regards,
Alex,
Typemock Support
answered by alex (17k points)
0 votes
Hi Alex, thank you for response

Yes indeed. All you say is good. But nothing about my question.
Imagine this. In a generic function some othe developer mocks an object such that one property would return always some value. I can not acces this function.
In a more specific function I use the mocked object and want to override the previous mocked value(cancel the previus value) without modifing the generic function. I also want to keep all other moked properties/methods of the object. I just want to override the mock for one propery.

So I want something like :

Isolate.WhenCalled(() => Object.Property).WillReturn(sameValue); --- in a not accesible function
and than in a diffrent function :
Isolate.WhenCalled(() => Object.Property).WillReturn(difefrentValue) -- I want here to clear/delete/reset the previous mock so that the first time (and all times) Object.Property will be called, diffrent value to be returned

In a more concise way of telling, I do not need default sequencing behavior in this case. I want it off. If this is stated of beeing default in the documentation, it means that this may be changed if one wants. So how to change it ?

Thank you
answered by psimion41@yahoo.com (260 points)
0 votes
Hi,

I now understand you question but this is not a standard testing issue.

Can you please describe the exact situation you are dealing with so i could help you find a solution.

Regard,
Alex,
Typemock Support.
answered by alex (17k points)
0 votes
+1 on this. [ img ]
answered by gavind (180 points)
0 votes
Hi Alex,

Suppose I want to mock an object of type XType.

public class XType
{
public string Property1 {get; private set;}
public string Property2 {get; private set;}
public string Property3 {get; private set;}
............

public string Method1 () {...}
public string Method2() {...}
...........
}

I have these tests :

public void Test1 () { XType x = MockObject(); Isolate.WhenCalled(() => x.Property1).WillReturn("Special value 1") ........... }
public void Test2() { XType x = MockObject(); Isolate.WhenCalled(() => x.Property2).WillReturn("Special value 2") ............ }
public void Test3() { XType x = MockObject(); Isolate.WhenCalled(() => x.Property3).WillReturn("Special value 3") ............... }
public void Test4() { XType x = MockObject(); Isolate.WhenCalled(() => x.Method1).WillReturn("Special method value 1") .............. }
public void Test5() { XType x = MockObject(); Isolate.WhenCalled(() => x.Method2).WillReturn("Special method value 2") ................ }
..............

and MockObject function :

function XType MockObject()
{
XType x = Isolate.Fake.Instance<XType >(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => x.Property1).WillReturn("Default value 1");
Isolate.WhenCalled(() => x.Property2).WillReturn("Default value 2");
Isolate.WhenCalled(() => x.Property3).WillReturn("Default value 2");
Isolate.WhenCalled(() => x.Method1).WillReturn("Default method value 1");
Isolate.WhenCalled(() => x.Method2).WillReturn("Default method value 2");
....................
}

Now......when I run Test1 I want that the mocked value for Property1 to be "Special value 1" and not "Default value 1"
Same for the others

I do not want to define parameters for the function MockObject because there will be to much parameteres......end simply........it would be stupid. Like this :
function MockObject(bool mockProperty1, bool mockProperty2........... )
{
XType x = Isolate.Fake.Instance<XType >(Members.ReturnRecursiveFakes);
if (mockProperty1)
Isolate.WhenCalled(() => x.Property1).WillReturn("Default value 1");
...................................
...................................

}

Also I do not want to make a dummy call in each test function like this :

public void Test1 ()
{
XType x = MockObject();
var dummy = x.Property1;
Isolate.WhenCalled(() => x.Property1).WillReturn("Special value 1")
...........
}

So.......can TypeMock handle this simple and very often situation ?
answered by psimion41@yahoo.com (260 points)
0 votes
Hi.

What you're trying to do is not supported in Isolator, with a good reason.

Tests should be plain about what thy are doing. When you try to "override" behaior, you make the test more complex, and even exposed to bugs.
What I suggest is that you collect all the common setup (that doesn't require special overriding) in a method, for exmple "Initialize". Then in every test, you'll add the special cases.
If you find that there are no common operations, just special ones, that means they weren't supposed to be together anyway. That means, if that method is [TestInitialize] or equivalent, remove the attribute and call it from the requiring tests.
answered by alex (17k points)
0 votes
Thank you Alex.

"When you try to "override" behaior, you make the test more complex, and even exposed to bugs. "
Is this a "good reason", are you kiding me ? This is the developer's concern, not typemock's concern, actually what you are saying is that building spaceships is wrong because "testing" the universe is complex and exposed to bugs :)
Typemock should provide the tools, than the developer decides what and how to do.

It is a weakness of typemock, but something that could not be so hard to repair in next versions.
I am convinced that this will happen in next versions, and then I will come back to you and ask you about "the good reason" TypeMock had not to implement this earlier :)

Thank you again Alex for your interest.
answered by psimion41@yahoo.com (260 points)
...