Reputation: 685
I am getting the error:
System.ArgumentException: Method HttpClient.SendAsync is public. Use strong-typed Expect overload instead: mock.Setup(x => x.SendAsync());
While trying to run the following code:
autoMock.Mock<HttpClient>()
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(httpResponseMessage)
.Verifiable();
What may I be missing here?
Upvotes: 2
Views: 2413
Reputation: 22679
HttpRequestHandler
:(You might need to pass MockBehavior.Strict
to the Mock<T>
ctor)
var mockedHandler = new Mock<HttpMessageHandler>();
var expectedResponse = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"..."),
};
mockedHandler
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(expectedResponse);
mockedHandler
when you instantiate the HttpClient
instancevar httpClient = new HttpClient(handlerMock.Object);
It is worth to take look at the MockHttp library, it makes your mocking lot easier.
Upvotes: 3