Xunit 集成测试 dotnet 6 System.Net.Http.HttpRequestException:响应状态代码未指示成功:415(不支持的媒体类型)

发布于 2025-01-14 05:10:49 字数 1487 浏览 4 评论 0原文

您好,我正在学习集成测试,我想使用 xunit 和 WebApplicationFactory 从我的控制器测试“POST”方法 但我收到此异常

错误消息: System.Net.Http.HttpRequestException:响应状态代码不指示成功:415(不支持的媒体类型)。 堆栈跟踪: 在 System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()

这是我的测试代码:

using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

namespace integrationtest.IntegrationTests;

public class ControllerTest  : IClassFixture<TestingWebAppFactory<Program>>
{
   private readonly HttpClient _client;
   public ControllerTest(TestingWebAppFactory<Program> factory) 
       => _client = factory.CreateClient();

   

   [Fact]
   public async Task AddInternshipMemberTest()
   {
       var postRequest = new HttpRequestMessage(HttpMethod.Post, "/api/InternshipMember/AddInternshipMember");

       var formModel = new Dictionary<string, string>
       {
           { "var1", "1"},
           { "var2", "0"},
           { "var3", "135"},
           { "var4", "1"},
           { "var5", "1"},
           { "var6", "1"},
           { "var7", "3" }
       };
       postRequest.Content = new FormUrlEncodedContent(formModel); 
       var response = await _client.SendAsync(postRequest); 
               
       response.EnsureSuccessStatusCode(); 
               
       var responseString = await response.Content.ReadAsStringAsync(); 
               
       Assert.Contains("1", responseString); 
   }
}

任何人都可以帮助我为什么会收到该错误吗?

Hello I'm learning integration testing and I want to test a 'POST' method from my controller using xunit and WebApplicationFactory
But I'm getting this exception

Error Message:
System.Net.Http.HttpRequestException : Response status code does not indicate success: 415 (Unsupported Media Type).
Stack Trace:
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()

This is my code for the test:

using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

namespace integrationtest.IntegrationTests;

public class ControllerTest  : IClassFixture<TestingWebAppFactory<Program>>
{
   private readonly HttpClient _client;
   public ControllerTest(TestingWebAppFactory<Program> factory) 
       => _client = factory.CreateClient();

   

   [Fact]
   public async Task AddInternshipMemberTest()
   {
       var postRequest = new HttpRequestMessage(HttpMethod.Post, "/api/InternshipMember/AddInternshipMember");

       var formModel = new Dictionary<string, string>
       {
           { "var1", "1"},
           { "var2", "0"},
           { "var3", "135"},
           { "var4", "1"},
           { "var5", "1"},
           { "var6", "1"},
           { "var7", "3" }
       };
       postRequest.Content = new FormUrlEncodedContent(formModel); 
       var response = await _client.SendAsync(postRequest); 
               
       response.EnsureSuccessStatusCode(); 
               
       var responseString = await response.Content.ReadAsStringAsync(); 
               
       Assert.Contains("1", responseString); 
   }
}

Can anyone help why i'm getting that error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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