2

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

我还通过从我的业务逻辑类调用它开始使用它,该类使用此通用PostAsync方法将 HTTP 调用发布到正文中包含内容的第 3 方。它完美地工作。

但是,当我尝试测试它时,我失败了!实际上,当我尝试调试(测试模式)时,当调试器在业务类中null到达这一行时,即使使用假对象和模拟,我也会得到响应,尽管它在调试模式下正常工作而无需测试/模拟。var result = await _httpService.PostAsync("https://test.com/api", content);Processor

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;
    }
}

商务课程:

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;
    }
}

测试类:

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);
    }
}
4

1 回答 1

3

您的问题在于模拟设置:

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

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

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

PS null 响应 PostAsync 意味着该方法具有默认设置,这意味着它将返回默认值

于 2019-12-08T17:21:13.937 回答