Created
May 31, 2016 08:06
-
-
Save bymyslf/6e24e895eadabab7c2b8320103d9570d to your computer and use it in GitHub Desktop.
Moq extension methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Threading.Tasks; | |
using Moq.Language; | |
using Moq.Language.Flow; | |
public static class MoqExtensions | |
{ | |
//see: http://haacked.com/archive/2010/11/24/moq-sequences-revisited.aspx/ | |
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 void ReturnsInOrderAsync<T, TResult>(this ISetup<T, Task<TResult>> setup, params object[] results) | |
where T : class | |
{ | |
var queue = new Queue(results); | |
setup.Returns(async () => | |
{ | |
var result = queue.Dequeue(); | |
if (result is Exception) | |
{ | |
throw result as Exception; | |
} | |
await Task.Yield(); | |
return (TResult)result; | |
}); | |
} | |
public static IReturnsResult<TMock> ReturnsYieldAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock, TResult value) where TMock : class | |
{ | |
return mock.Returns(async () => | |
{ | |
await Task.Yield(); | |
return value; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment