我在使用Polly
和HttpClient
时执行单元测试时遇到问题。
具体来说,Polly 和 HttpClient 用于 ASP.NET 核心 Web API 控制器,链接如下:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests
https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory
问题(1 和 2)在底部指定。我想知道这是否是使用Polly
和HttpClient
的正确方法.
配置服务
配置 Polly 策略并单独自定义 HttpClient,CustomHttpClient
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddHttpClient<HttpClientService>()
.AddPolicyHandler((service, request) =>
HttpPolicyExtensions.HandleTransientHttpError()
.WaitAndRetryAsync(3,
retryCount => TimeSpan.FromSeconds(Math.Pow(2, retryCount)))
);
}
汽车控制器
CarController 依赖于HttpClientService
,它是由框架自动注入的,无需显式注册。
请注意,HttpClientService
会导致单元测试出现问题,因为Moq
无法模拟非虚拟方法,稍后会提到。
[ApiVersion("1")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public class CarController : ControllerBase
{
private readonly ILog _logger;
private readonly HttpClientService _httpClientService;
private readonly IOptions<Config> _config;
public CarController(ILog logger, HttpClientService httpClientService, IOptions<Config> config)
{
_logger = logger;
_httpClientService = httpClientService;
_config = config;
}
[HttpPost]
public async Task<ActionResult> Post()
{
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
string body = reader.ReadToEnd();
var statusCode = await _httpClientService.PostAsync(
"url",
new Dictionary<string, string>
{
{"headerID", "Id"}
},
body);
return StatusCode((int)statusCode);
}
}
}
HttpClientService
Simiar toCarController
,HttpClientService
有一个单元测试的问题,因为HttpClient
的后期异步不能被模拟。 框架会自动注入HttpClient
,无需显式注册。
public class HttpClientService
{
private readonly HttpClient _client;
public HttpClientService(HttpClient client)
{
_client = client;
}
public async Task<HttpStatusCode> PostAsync(string url, Dictionary<string, string> headers, string body)
{
using (var content = new StringContent(body, Encoding.UTF8, "application/json"))
{
foreach (var keyValue in headers)
{
content.Headers.Add(keyValue.Key, keyValue.Value);
}
var response = await _client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
return response.StatusCode;
}
}
问题1
单元测试:Moq
不能模拟HttpClientService
的PostAsync
方法。我可以将其更改为虚拟,但我想知道它是否是最佳选择。
public class CarControllerTests
{
private readonly Mock<ILog> _logMock;
private readonly Mock<HttpClient> _httpClientMock;
private readonly Mock<HttpClientService> _httpClientServiceMock;
private readonly Mock<IOptions<Config>> _optionMock;
private readonly CarController _sut;
public CarControllerTests() //runs for each test method
{
_logMock = new Mock<ILog>();
_httpClientMock = new Mock<HttpClient>();
_httpClientServiceMock = new Mock<HttpClientService>(_httpClientMock.Object);
_optionMock = new Mock<IOptions<Config>>();
_sut = new CarController(_logMock.Object, _httpClientServiceMock.Object, _optionMock.Object);
}
[Fact]
public void Post_Returns200()
{
//System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member
_httpClientServiceMock.Setup(hc => hc.PostAsync(It.IsAny<string>(),
It.IsAny<Dictionary<string, string>>(),
It.IsAny<string>()))
.Returns(Task.FromResult(HttpStatusCode.OK));
}
}
}
问题2
单元测试:与HttpClientService
类似,Moq
不能模拟HttpClient
的PostAsync
方法。
public class HttpClientServiceTests
{
[Fact]
public void Post_Returns200()
{
var httpClientMock = new Mock<HttpClient>();
//System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member
httpClientMock.Setup(hc => hc.PostAsync("", It.IsAny<HttpContent>()))
.Returns(Task.FromResult(new HttpResponseMessage()));
}
}
ASP.NET 核心接口 2.2
更新
更正了 CustomHttpClient 到 HttpClientService 的拼写错误
更新 2
问题 2 的解决方案
假设CustomHttpClient
是一个错别字,HttpClientService
是实际的依赖关系,控制器与实现问题紧密耦合,正如您已经经历的那样,很难单独测试(单元测试)。
将这些凝固物封装在抽象之后
public interface IHttpClientService {
Task<HttpStatusCode> PostAsync(string url, Dictionary<string, string> headers, string body);
}
public class HttpClientService : IHttpClientService {
//...omitted for brevity
}
可以在测试时替换。
重构控制器以依赖于抽象而不是具体实现
public class CarController : ControllerBase {
private readonly ILog _logger;
private readonly IHttpClientService httpClientService; //<-- TAKE NOTE
private readonly IOptions<Config> _config;
public CarController(ILog logger, IHttpClientService httpClientService, IOptions<Config> config) {
_logger = logger;
this.httpClientService = httpClientService;
_config = config;
}
//...omitted for brevity
}
更新服务配置以使用允许注册抽象及其实现的重载
public void ConfigureServices(IServiceCollection services) {
services.AddHttpClient();
services
.AddHttpClient<IHttpClientService, HttpClientService>() //<-- TAKE NOTE
.AddPolicyHandler((service, request) =>
HttpPolicyExtensions.HandleTransientHttpError()
.WaitAndRetryAsync(3,
retryCount => TimeSpan.FromSeconds(Math.Pow(2, retryCount)))
);
}
这负责重新调整代码以使其对测试更友好。
下面显示了现在可以单独对控制器进行单元测试
public class CarControllerTests {
private readonly Mock<ILog> _logMock;
private readonly Mock<IHttpClientService> _httpClientServiceMock;
private readonly Mock<IOptions<Config>> _optionMock;
private readonly CarController _sut;
public CarControllerTests() //runs for each test method
{
_logMock = new Mock<ILog>();
_httpClientServiceMock = new Mock<IHttpClientService>();
_optionMock = new Mock<IOptions<Config>>();
_sut = new CarController(_logMock.Object, _httpClientServiceMock.Object, _optionMock.Object);
}
[Fact]
public async Task Post_Returns200() {
//Arrange
_httpClientServiceMock
.Setup(_ => _.PostAsync(
It.IsAny<string>(),
It.IsAny<Dictionary<string, string>>(),
It.IsAny<string>())
)
.ReturnsAsync(HttpStatusCode.OK);
//Act
//...omitted for brevity
//...
}
}
请注意,不再需要完成测试的HttpClient
。
控制器还需要确保安排其他依赖项才能使测试流动,但这目前不在问题的范围之内。