Xunit 集成测试 dotnet 6 System.Net.Http.HttpRequestException:响应状态代码未指示成功:415(不支持的媒体类型)
您好,我正在学习集成测试,我想使用 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 技术交流群。

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