Overview

Since HttpClient is not backed by an interface, the approach to get an HttpClient for testing is different:

  1. Mock HttpMessageHandler:
    var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
    
  2. Set up the mock:
    // Call Protected() since since HttpMessageHandler's constructor is protected:
    handlerMock.Protected()
            // Override HttpMessageHandler's protected internal abstract SendAsync method:
            .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content = new StringContent(httpGetResponseStringContent)
            });
    
  3. Pass the mocked HttpMessageHandler to the HttpClient constructor to get a test HttpClient:
    var httpClient = new HttpClient(handlerMock.Object);
    
  4. Pass that HttpClient to the system under test that would normally receive an injected HttpClient instance:
    var sut = new SomeService(httpclient, ...);