当状态代码为错误500时,在单位测试中将其作为内容传递给HTTPRESPONSEMESSAGE?

发布于 2025-02-06 15:22:53 字数 573 浏览 1 评论 0原文

我正在开发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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文