I get a Moq object to return different values on successive calls to a method. This is done by this extension method:
public static void ReturnsInOrder<T, TResult>(this ISetup<T, TResult> setup, params TResult[] results) where T : class
{
setup.Returns(new Queue<TResult>(results).Dequeue);
}
Now I want one of the calls to throw an exception while others return something. Has anyone done this before?
If i do this
mock.Setup(m => m.SomeMethod())
.Throws(new Exception());
mock.Setup(m => m.SomeMethod())
.Returns("ok");
then the the first setup is overwritten and only the second setup persists.
Nowadays Moq (version 4+) supports this through its SetupSequence
method. See this post for an introduction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With