[英]Mock HttpClient.SendAsync to return response with Content not null
I'm trying to mock var response = await httpClient.SendAsync(request, CancellationToken.None);
我正在尝试模拟
var response = await httpClient.SendAsync(request, CancellationToken.None);
but my response.Content
is always null
.但我的
response.Content
始终是null
。
My mock looks like...我的模拟看起来像......
var httpResponseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
httpResponseMessage.Content = new StringContent("test content", System.Text.Encoding.UTF8, "application/json");
A.CallTo(() => httpClient.SendAsync(A.Fake<HttpRequestMessage>(), CancellationToken.None)).Returns(Task.FromResult(httpResponseMessage));
it seems like it is properly mocked but the response.Content is null but the status code - for example - reflects what I set in the test.似乎它被正确地模拟了,但 response.Content 是 null 但状态代码 - 例如 - 反映了我在测试中设置的内容。
I'm sure one of you guys out here have encountered this problem, all help will be greatly appreciated.我相信你们中的一个人已经遇到过这个问题,所有的帮助将不胜感激。 Thanks.
谢谢。
Likely the call to SendAsync
isn't being matched.对
SendAsync
的调用可能不匹配。 I see you configured your fake to respond to我看到你配置了你的假货来回应
A.CallTo(() => httpClient.SendAsync(A.Fake<HttpRequestMessage>(), CancellationToken.None))
But it's very unlikely that your production code is passing a first argument that compares as equal to the A.Fake<HttpRequestMessage>()
argument you have here.但是,您的生产代码不太可能传递与您在此处的
A.Fake<HttpRequestMessage>()
参数比较相等的第一个参数。
Did you instead mean你的意思是
A.CallTo(() => httpClient.SendAsync(A<HttpRequestMessage>.Ignored, CancellationToken.None))
(or equivalently A<HttpRequestMessage>._
)? (或等效
A<HttpRequestMessage>._
)?
You can read about how arguments are matched on the Argument constraints page.您可以在参数约束页面上阅读有关 arguments 如何匹配的信息。 Specifically, see how to match any argument at the Ignoring arguments subtopic.
具体来说,请参阅忽略 arguments子主题中的任何参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.