So in my last post I showed a nifty Typemock extension for doing repetition within Typemock's AAA syntax on the WhenCalled method. When I wrote that extension it was only done in a rush and it had 1 flaw, you couldn't do method chaining to do the n+1 action, you had to do it on a separate line.
Well I spent another 5 minutes on it and added this feature (plus a repeat on CallOriginal). Here's the updated extension set:
public static class Extensions { public static ActionRepeater<TReturn> WillReturnRepeat<TReturn>(this IPublicNonVoidMethodHandler<TReturn> ret, TReturn value, int numberOfReturns) { for (var i = 0; i < numberOfReturns; i++) ret.WillReturn(value);<span class="keyword">return new</span> <span class="const">ActionRepeater</span><TReturn>(ret); } <span class="keyword">public static</span> <span class="const">ActionRepeater</span><TReturn> CallOriginalRepeat<TReturn>(this <span class="const">IPublicNonVoidMethodHandler</span><TReturn> ret, <span class="keyword">int</span> numberOfReturns) { <span class="keyword">for</span> (<span class="keyword">var</span> i = 0; i < numberOfReturns; i++) ret.CallOriginal(); <span class="keyword">return new</span> <span class="const">ActionRepeater</span><TReturn>(ret); } } <span class="keyword">public class</span> <span class="const">ActionRepeater</span><TReturn> { <span class="keyword">private</span> <span class="const">IPublicNonVoidMethodHandler</span><TReturn> _actionRepeater; <span class="keyword">public</span> <span class="const">ActionRepeater</span><TReturn>(<span class="const">IPublicNonVoidMethodHandler</span><TReturn> actionRepeater) { _actionRepeater = actionRepeater; } <span class="keyword">public</span> <span class="const">IPublicNonVoidMethodHandler</span><TReturn> AndThen() { return _actionRepeater; } }
I'll admit that I have made it a touch verbose to use, but I think it's better to convey what is happening to other people reading the tests (it's a lot like Rhino Mocks in verboseness I guess). So to use it now all you need to do is:
Isolate.WhenCalled(() => someMock.SomeMethod()).WillReturnRepeat(true, 3).AndThen().CallOriginal(); //or, chained repeats! Isolate.WhenCalled(() => someMock.SomeOtherMethod()).WillReturnRepeat("Hello World", 2).AndThen().WilLReturnRepeat("Good-bye World", 2).AndThen().CallOriginal();
Makes for some really crazy mocks ;)
PS: You can use the Repeat extensions with a repeat number of 1 if you want just method chaining too:
Isolate.WhenCalled(() => someMock.SomeMethod()).WillReturnRepeat(true, 1).CallOriginalRepeat(1).ReturnRecursiveFakes();