I have a controller with a simple conditional statement that checks a value and then returns a BadRequest should that value not match what is expected.
public async Task<IActionResult> SendCarDetails(string carValue)
{
try
{
if (carValue== "0")
{
return BadRequest("Car value cannot be 0");
}
}
catch (Exception ex)
{
return StatusCode(500);
}
}
To test this simple condition I wrote a unit test, although it works I feel that it's a little disconnected from the above method and therefore is not sufficient to qualify as a test. This defines the value as being 0 and then checks it, I then create a new BadRequestResult and should that response match 400 then the test is passed.
[TestMethod]
public void SendCarDetails_Returns_400_When_Zero()
{
string paymentValue = "0";
if (paymentValue == "0")
{
var response = new BadRequestResult();
Assert.AreEqual(StatusCodes.Status400BadRequest, response.StatusCode);
}
}
Can someone guide me on writing a unit test for my controller condition or is my approach above accurate?