Barış Velioğlu
Barış Velioğlu

Reputation: 5817

Mock HttpClientFactory to create a mocked HttpClient Using Moq Framework

I have method which contains a HttpClientFactory that creates a HttpClient. The method calls SendAsync method in it. I need to manipulate SendAsync method to send me a succeed message whatever it takes as an argument.

I want to test this method

public class TestService
{
    private readonly IHttpClientFactory _clientFactory;

    public TestService(IHttpClientFactory clientFactory)
    {
         _clientFactory = clientFactory;
    }

    public async Task<bool> TestMethod(string testdata)
    {
        var message = new HttpRequestMessage(); //SIMPLIFIED CODE
        var client = _clientFactory.CreateClient();
        var response = await client.SendAsync(message);

        if(response.IsSuccessStatusCode){
            return true;
        }else{
            return false;
        }
    }
}

What I try is

private readonly Mock<IHttpClientFactory> _mockHttpClientFactory;
private readonly Mock<HttpClient> _mockHttpClient;

[Fact]
public void TestMethod_ValidHttpRequest_ReturnsTrue()
{
    var httpClient = _mockHttpClient
                        .Setup(x => x.SendAsync(It.IsAny<HttpRequestMessage>()))
                        .ReturnsAsync(new HttpResponseMessage()
                        {
                            StatusCode = HttpStatusCode.OK,
                            Content = new StringContent("[{'id':1,'value':'1'}]"),
                        });

    var client = _mockHttpClientFactory.Setup(x => x.CreateClient()).Returns(httpClient); //DOESNT WORK
    
    var service = new TestService(_mockHttpClientFactory);
    var result = service.TestMethod("testdata");
    Assert.True(result.Result)
}

Upvotes: 5

Views: 6470

Answers (1)

I suggest to look into the following blog post from Gingster Ale. This example of the author shows you how you can mock calls to HttpClient:

// ARRANGE
var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
handlerMock
   .Protected()
   // Setup the PROTECTED method to mock
   .Setup<Task<HttpResponseMessage>>(
      "SendAsync",
      ItExpr.IsAny<HttpRequestMessage>(),
      ItExpr.IsAny<CancellationToken>()
   )
   // prepare the expected response of the mocked http call
   .ReturnsAsync(new HttpResponseMessage()
   {
      StatusCode = HttpStatusCode.OK,
      Content = new StringContent("[{'id':1,'value':'1'}]"),
   })
   .Verifiable();

// use real http client with mocked handler here
var httpClient = new HttpClient(handlerMock.Object)
{
   BaseAddress = new Uri("http://test.com/"),
};

var subjectUnderTest = new MyTestClass(httpClient);

// ACT
var result = await subjectUnderTest
   .GetSomethingRemoteAsync('api/test/whatever');

// ASSERT
result.Should().NotBeNull(); // this is fluent assertions here...
result.Id.Should().Be(1);

// also check the 'http' call was like we expected it
var expectedUri = new Uri("http://test.com/api/test/whatever");

handlerMock.Protected().Verify(
   "SendAsync",
   Times.Exactly(1), // we expected a single external request
   ItExpr.Is<HttpRequestMessage>(req =>
      req.Method == HttpMethod.Get  // we expected a GET request
      && req.RequestUri == expectedUri // to this uri
   ),
   ItExpr.IsAny<CancellationToken>()
);

Also, if you have the freedom to use something else than HttpClient, I recommend to take a look into Flurl.

Upvotes: 5

Related Questions