OmarBouattour
New member
- Joined
- Mar 16, 2022
- Messages
- 3
- Programming Experience
- Beginner
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
System.Net.Http.HttpRequestException : Response status code does not indicate success: 400 (Bad Request).
This is my Test:
Can anyone help why i'm getting that error?
System.Net.Http.HttpRequestException : Response status code does not indicate success: 400 (Bad Request).
This is my Test:
C#:
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);
postRequest.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
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?
Last edited by a moderator: