Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Created December 16, 2015 14:57
Show Gist options
  • Save lkaczanowski/04698ad1093004012adc to your computer and use it in GitHub Desktop.
Save lkaczanowski/04698ad1093004012adc to your computer and use it in GitHub Desktop.
HttpMessageHandler mock helper
internal static class MockHttpMessageHandlerHelper
{
public static void SetupSendAsync(this Mock<HttpMessageHandler> handler, Func<HttpResponseMessage> sendAsyncResultFunc)
{
handler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.Returns(Task<HttpResponseMessage>.Factory.StartNew(sendAsyncResultFunc));
}
public static void VerifySendAsync(this Mock<HttpMessageHandler> handler, Times times)
{
handler.Protected()
.Verify<Task<HttpResponseMessage>>("SendAsync", times, ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>());
}
public static void VerifySendAsync(this Mock<HttpMessageHandler> handler, Expression<Func<HttpRequestMessage, bool>> requestMessagePredicate, Times times)
{
handler.Protected()
.Verify<Task<HttpResponseMessage>>("SendAsync", times, ItExpr.Is<HttpRequestMessage>(requestMessagePredicate), ItExpr.IsAny<CancellationToken>());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment