找到了一个好东东:Moq.Contrib.HttpClient
在测试中 mock HttpClient 的试验代码如下
[Theory]
[InlineData(1, 2, 3)]
public async Task GetPublicPostIds_test(params int[] postIds)
{
var publicPostIds = postIds.ToList();
publicPostIds.Remove(Random.Shared.Next(1, 3));
var handler = new Mock<HttpMessageHandler>();
var httpClient = handler.CreateClient();
httpClient.BaseAddress = new Uri("http://blog_api");
handler.SetupRequest(HttpMethod.Post, "http://blog_api/v2/blogposts/public/ids")
.ReturnsJsonResponse(publicPostIds);
var actualPostIds = await new BlogClient(httpClient, _logger).BlogPosts
.GetPublicPostIds(postIds);
Assert.NotEmpty(actualPostIds);
actualPostIds.Should().BeEquivalentTo(publicPostIds);
}