Skip to content

Instantly share code, notes, and snippets.

@bugthesystem
Last active October 10, 2015 19:07
Show Gist options
  • Save bugthesystem/3736662 to your computer and use it in GitHub Desktop.
Save bugthesystem/3736662 to your computer and use it in GitHub Desktop.
Moq Sequences Revisited from http://haacked.com/archive/2010/11/24/moq-sequences-revisited.aspx Added ReturnsAsync method for c# 5.0 tests
public static class MoqExtensions
{
public static void ReturnsInOrder<T, TResult>(this ISetup<T, TResult> setup,
params object[] results) where T : class {
var queue = new Queue(results);
setup.Returns(() => {
var result = queue.Dequeue();
if (result is Exception) {
throw result as Exception;
}
return (TResult)result;
});
}
public static IReturnsResult<TMock> ReturnsAsync<TMock, TResult>(
this IReturns<TMock, Task<TResult>> setup, TResult value)
where TMock : class
{
return setup.Returns(Task.FromResult(value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment