c# - Moq.Verify of HttpClient passes even when it should fail - Stack Overflow

After a bunch of research from SO and other places, I have a unit test that looks like this:Content =

After a bunch of research from SO and other places, I have a unit test that looks like this:

Content = new StringContent("{" +
    "\"data\": null" +
"}")

Mock<HttpClient> httpClientMock = new();
var httpMessageHandlerMock = new Mock<HttpMessageHandler>();
httpMessageHandlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
    "SendAsync",
    ItExpr.IsAny<HttpRequestMessage>(),
    ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(expectedResponse);

_httpClientFactoryMock.Setup(fact => fact.CreateClient(It.IsAny<string>())).Returns(new HttpClient(httpMessageHandlerMock.Object));
DealerLocationService s = new(_httpClientFactoryMock.Object, _fakeOptions, _loggerMock.Object);
_ = await s.GetClosest("us", "12345");
httpMessageHandlerMock.Protected().Verify(
    "SendAsync",
    Times.Between(0, 1, Moq.Range.Inclusive),
    ItExpr.Is<HttpRequestMessage>(req => req.RequestUri.AbsolutePath == "/us/123456"),
    ItExpr.IsAny<CancellationToken>());

The problem is that this code passes in all cases. As written, it should fail -- the path it would receive is /US/12345. The test passes when I check for that at the end... but it also passes when I check for /us/123456 as I do above.

I could put anything in that ItExpr.Is() call in the Verify() step, and the test passes. What I want to do is make sure that the request the HttpClient got was what I built -- complete with case-sensitivity.

Can someone please point me to what I'm missing?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743737746a4498571.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信