I\'m in the process of building an ASP.NET Core WebAPI and I\'m attempting to write unit tests for the controllers. Most examples I\'ve found are from the older WebAPI/WebA
Assuming something like the
public IActionResult GetOrders() {
var orders = repository.All();
return Ok(orders);
}
the controller in this case is returning an OkObjectResult class.
Cast the result to the type of what you are returning in the method and perform your assert on that
[Fact]
public void GetOrders_WithOrdersInRepo_ReturnsOk() {
// arrange
var controller = new OrdersController(new MockRepository());
// act
var result = controller.GetOrders();
var okResult = result as OkObjectResult;
// assert
Assert.IsNotNull(okResult);
Assert.AreEqual(200, okResult.StatusCode);
}