I have on several occasions wished that I could setup expectations in Moq for subsequent calls to a method. For example, I might want Moq to return one value the first time a method is called, but a different value the second and third times. Phil Haack has a nice way to achieve this, but his implementation only allows you to return a result. What if you need something more advanced? What if you want to return a value on the first call, throw an exception on the second, and return a value again on the third? I ran into exactly this situation while testing out some error-handling logic in a class. Here’s how I achieved it.
public static class MoqExtensions { public static IReturnsResult<TMockType> Returns<TMockType, TReturnType>(this IReturns<TMockType, TReturnType> expression, params Func<TReturnType>[] results) where TMockType : class { int nextResult = 0; return expression.Returns(() => results[nextResult++]()); } }
This adds a new Returns method you can use with Moq in place of one of the built-in Return methods. It allows you to specify an arbitrary number of Func’s to be executed. Instead of using Moq to return the Func’s directly, it creates a new Func (via a lambda expression) that will iterate over the result Func’s specified. It does this via a closure around the results and the index of the next result. Note that it accepts a ‘params’ array, so calling it still looks (fairly) clean:
[Test] public void ContinuesWorkingIfErrorEventIsHandled() { mResultSet.Setup(r => r.GetNextResult()).Returns((Func<IResult>)(() => { throw new NotImplementedException(); }), () => null); //Run... mResultSet.Verify(r => r.GetNextResult(), Times.Exactly(2)); }
That’s all there is to it!
It would have been a very simple task to require subscription to a newsletter in order to download the ebook.
Helo, beeaaauuutiful Blog …Bookmarked! I hope you have a nice day! Tasmania, Australia <a href=”http://www.Portrait-Oil-Painting.Co.Cc”>Museum Oil Painting</a>
You raise a lot of questions in my mind; you wrote a good post, but this post is also thought provoking, and I will have to think about it some more; I will return soon.
You made some good points there. I did a exploration on the issue and found most people will agree with your blog.
Ya visual studio have really more feature.I have already work on it.It’s really very comfortable version and have more functionality that we use to make our application wide.
I could not agree more.
Hello, this is really interesting. Can you show me additional resources to read more ??
I am always looking into stuff about a pc or the industry that I do not know about. This is the type of thing I love to read about. Nice share! Thanks.
Hey mate, your blog’s design is great and i like it. Your posts are nice. Please continue this great work. Take Care…
Useful information, many thanks to the author. It is puzzling to me now, but in general, the usefulness and significance is overwhelming. Very much thanks again and good luck!
jeez a lot of of the responses readers distribute are a bit spacey, alot of times i wonder whether they honestly read the items and items before placing a comment or whether they just read the title of the article and compose the very first thought that drifts into their heads. in any event, it is actually pleasurable to look over sensible commentary every now and then rather than the same exact, outdated oppinion vomit that i invariably notice on the net