A nifty Typemock extension

3 March 2009 · 1 minute read · Unit Testing · Typemock

Using AAA with Typemock there's a bit of a problem if you want to repeat the returned value a number of times before then doing something different. It's very useful if you are accessing a mocked object within a loop and want to know the number of loop execution.

So I've put together a simple little Typemock extension (but I'm sure it'd adaptable for any mock framework supporting AAA):

public static void WillReturnRepeat<TReturn>(this IPublicNonVoidMethodHandler ret, TReturn value, int numberOfReturns)
{
    for (var i = 0; i < numberOfReturns; i++)
        ret.WillReturn(value);
}

You then just use it like this:

Isolate.WhenCalled(() => mockObject.SomeMethod()).WillReturnRepeat(true, 3);
Isolate.WhenCalled(() => mockObject.SomeMethod()).CallOriginal();

So the mock will return true 3 times and it will do the original call (for the purpose of this demo we'll assume it would return false).

Anyone else got some nifty Typemock extensions?  


Published: 2017-10-23 09:53:42 +1100 +1100, Version: 4113ab4