模拟 IHttpClientFactory - xUnit C#

Mocking IHttpClientFactory - xUnit C#

我试图在我的项目中构建一个通用的 HTTP 服务(c# with .net core 2.1),我已经按照下面的代码片段 HttpService.

完成了它

我也开始使用它,从我的业务逻辑 class 中调用它,它使用这个通用的 PostAsync 方法 post 对第三方的 HTTP 调用,其中内容在正文中.它完美运行。

但是,当我尝试测试它时,我失败了! 实际上,当我尝试调试(测试模式)时,当调试器在业务 class Processor 中到达此行 var result = await _httpService.PostAsync("https://test.com/api", content); 时,我得到 null 响应,即使使用假对象和模拟,尽管它在没有 testing/mocking.

的调试模式下正常工作

HTTP 服务:

public interface IHttpService
{
    Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content);
}

public class HttpService : IHttpService
{
    private readonly IHttpClientFactory _httpClientFactory;

    public HttpService(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }

    public async Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
    {
        var httpClient = _httpClientFactory.CreateClient();
        httpClient.Timeout = TimeSpan.FromSeconds(3);
        var response = await httpClient.PostAsync(requestUri, content).ConfigureAwait(false);
        response.EnsureSuccessStatusCode();

        return response;
    }
}

商业class:

public class Processor : IProcessor
{
    private readonly IHttpService _httpService;

    public Processor() { }

    public Processor(IHttpService httpService, IAppSettings appSettings)
    {
        _httpService = httpService;
    }

    public async Task<HttpResponseMessage> PostToVendor(Order order)
    {
        // Building content
        var json = JsonConvert.SerializeObject(order, Formatting.Indented);
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        // HTTP POST
        var result = await _httpService.PostAsync("https://test.com/api", content); // returns null during the test without stepping into the method PostAsyn itself

        return result;
    }
}

测试class:

public class MyTests
{
    private readonly Mock<IHttpService> _fakeHttpMessageHandler;
    private readonly IProcessor _processor; // contains business logic
    private readonly Fixture _fixture = new Fixture();

    public FunctionTest()
    {
        _fakeHttpMessageHandler = new Mock<IHttpService>();
        _processor = new Processor(_fakeHttpMessageHandler.Object);
    }

    [Fact]
    public async Task Post_To_Vendor_Should_Return_Valid_Response()
    {
        var fakeHttpResponseMessage = new Mock<HttpResponseMessage>(MockBehavior.Loose, new object[] { HttpStatusCode.OK });

        var responseModel = new ResponseModel
        {
            success = true,
            uuid = Guid.NewGuid().ToString()
        };

        fakeHttpResponseMessage.Object.Content = new StringContent(JsonConvert.SerializeObject(responseModel), Encoding.UTF8, "application/json");

        var fakeContent = _fixture.Build<DTO>().Create(); // DTO is the body which gonna be sent to the API
        var content = new StringContent(JsonConvert.SerializeObject(fakeContent), Encoding.UTF8, "application/json");

        _fakeHttpMessageHandler.Setup(x => x.PostAsync(It.IsAny<string>(), content))
            .Returns(Task.FromResult(fakeHttpResponseMessage.Object));

        var res = _processor.PostToVendor(fakeContent).Result;
        Assert.NotNull(res.Content);
        var actual = JsonConvert.SerializeObject(responseModel);
        var expected = await res.Content.ReadAsStringAsync();
        Assert.Equal(expected, actual);
    }
}

您的问题出在模拟设置中:

_fakeHttpMessageHandler.Setup(x => x.PostAsync(It.IsAny<string>(), content))
        .Returns(Task.FromResult(fakeHttpResponseMessage.Object));

PostAsync 方法的第二个参数应为 content,但由于 StringContent 是引用类型,因此您在 mock 中设置的 content 不同于您在处理器中创建的 content。如果将其更改为下一个,它应该会按预期工作:

    _fakeHttpMessageHandler.Setup(x => x.PostAsync(It.IsAny<string>(), It.IsAny<StringContent>()))
        .Returns(Task.FromResult(fakeHttpResponseMessage.Object));

P.S。对 PostAsync 的空响应意味着该方法具有默认设置,这意味着它将 return 默认值