I am tring to test one web api method in asp.net core 2.
Web api
method:
public IActionResult Delete(int id)
{
try
{
var success = sportsService.DeleteSport(id);
if (!success)
{
return new NotFoundResult();
}
return new OkResult();
}
catch (Exception e)
{
return new StatusCodeResult(400);
}
}
Unit testing part:
private Mock<ISportsService> mockService;
[SetUp]
public void Setup()
{
mockService.SetupSequence(x => x.DeleteSport(It.IsAny<int>())).
Returns(true). // OkResult();
Returns(false). // NotFoundResult()
Throws(new Exception()); //Exception
}
Test method looks like this. What I am not clear about with this method is, what the right approach to testing the method is?
[Test]
public void TestStrategyControllerWithDelete()
{
var result = controller.Delete(STRAT_ID);
var okResult = result as ObjectResult;
Assert.AreEqual(HttpStatusCode.NotFound, okResult.StatusCode);
Assert.AreEqual(HttpStatusCode.OK, okResult.StatusCode);
Assert.AreEqual(400, okResult.StatusCode);
}
and i am okresult as null ! and not able to get okResult.StatusCode