I am trying to create an Integration Test for my .Net Core Web Api
But I am always getting a 400 Bad Request response. I am sharing details below
Here is my Controller method
public IActionResult UpdateProductById([FromBody]int id, string description) { var result = ProductService.UpdateProductById(id, description); if (result.Exception == null) return Ok(result); else return BadRequest(result.Exception.Message); } Here is my test class (which tries to post)
[Fact] public async Task UpdateProductById_Test_WithProduct() { var product = new { id = 1, description = "foo" }; var productObj= JsonConvert.SerializeObject(product); var buffer = System.Text.Encoding.UTF8.GetBytes(productObj); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var result = await _tester.Client.PostAsync("/api/1.0/UpdateProductById", byteContent); result.StatusCode.Should().Be(HttpStatusCode.OK); }