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
Hi,

I'm currently evaluating TypeMock.
I wish to set an expectation on how many times a method should be called on my mock like so:

Mock.Arrange(() => _cache["test"]).Returns(new object()).Occurs(5);

The only thing I could find was:

Isolate.Verify.WasCalledWithArguments(() => _cache["test"]);


I can't seem to find an out of the box method to just pass in the number of times I expect a method to be called. Is there something like this?


Thanks
James
asked by JamesKing (48.2k points)

1 Answer

0 votes
Hi James,

I'm not sure that I understand if you are trying to set expectation on the first 5 calls or just trying to verify that the method was called 5 times.
Both are possible with Typemock:


Typemock allows you to create sequenced behavior.
You can do:

Isolate.WhenCalled( () => _cache["test"] ).WillReturn (new object());
Isolate.WhenCalled( () => _cache["test"] ).WillReturn (new object());
Isolate.WhenCalled( () => _cache["test"] ).WillReturn (new object());
Isolate.WhenCalled( () => _cache["test"] ).WillReturn (new object());
Isolate.WhenCalled( () => _cache["test"] ).WillReturn (new object());
Isolate.WhenCalled( () => _cache["test"] ).CallOriginal();

The code above will make _cache["test"] return new object() for the first five calls and the original implementation will be called from the sixth call and on.


In order to assert that the method was called 5 times you should use:

var timesCalled = Isolate.Verify.GetTimesCalled(() => _cache["test"]);
Assert.AreEqual(5 , timesCalled);
answered by Shai Barak (1.5k points)
What if parameters passed don't matter?
...