A nifty Typemock extension

Tuesday, Mar 3, 2009 1 minute read Tags: Unit Testing Typemock
Hey, thanks for the interest in this post, but just letting you know that it is over 3 years old, so the content in here may not be accurate.

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?