I'm unit testing a code similar to the one below. There is one use case where I want the GetAsync to throw an exception to be caught in the catch block.
try
{
var response = await client.GetAsync(url, cancellationToken);
}
catch(Exception e)
{
_logger.LogError(e)
}
I'm mocking the HttpClient
var factory = new Mock<IHttpClientFactory>();
var httpClient = new HttpClient(handler)
{
BaseAddress = new Uri("http://localhost:7100")
}
factory.Setup(_ => _.CreateClient(It.IsAny<string>()))
.Returns(httpClient)
.Verifiable();
Here I'm mocking the HttpMessageHandler
var handler = new Mock<HttpMessageHandler>();
handler
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.Returns(GetHttpResponseMessage())
.Verifiable();
GetHttpResponseMessage() is the method where I put the logic to return the appropriate HttpResponseMassage.
HttpResponseMessage GetHttpResponseMessage(int code)
{
if(code == 200)
{
return new HttpResponseMessage{ StatusCode = HttpStatusCode.OK };
}
else if(code == 500)
{
//StatusCode.InternalServerError ???
//throw new Exception() ???
}
}
None of those 2 above is working. Neither one is causing an exception to be caught by the catch block.
Thanks for helping