当状态代码为错误500时,在单位测试中将其作为内容传递给HTTPRESPONSEMESSAGE?
我正在开发ASP.NET Core 6 Web API。
我正在尝试单元测试通过HTTPClient发送和接收响应的服务。 我正在通过嘲笑httpmessagehandler并构建httpresponsemessage来做到这一点。
当结果为200时 - 很明显 - 我将数据的JSON传递给内容。但是,响应代码为500何时呢?
当外部服务返回错误响应时,我无法弄清楚内容是什么?
var handlerMock = new Mock<HttpMessageHandler>();
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"[{ ""id"": 1, ""title"": ""Cool post!""}, { ""id"": 100, ""title"": ""Some title""}]"),
};
I am developing an asp.net core 6 web api.
I am trying to unit test a service which sends and receives responses through httpclient.
I am doing this through mocking HttpMessageHandler and constructing an HttpResponseMessage.
When the result is 200 OK - it is clear - I pass the json of the data to the Content. But what about when the response code is 500?
I can't figure out what the content is when the external service returns an error reponse?
var handlerMock = new Mock<HttpMessageHandler>();
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(@"[{ ""id"": 1, ""title"": ""Cool post!""}, { ""id"": 100, ""title"": ""Some title""}]"),
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论