Unit testing controller methods which return IActionResult

前端 未结 4 829
时光说笑
时光说笑 2020-12-05 09:07

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

4条回答
  •  日久生厌
    2020-12-05 09:50

    You can also do cool things like:

        var result = await controller.GetOrders();//
        var okResult = result as ObjectResult;
    
        // assert
        Assert.NotNull(okResult);
        Assert.True(okResult is OkObjectResult);
        Assert.IsType(okResult.Value);
        Assert.Equal(StatusCodes.Status200OK, okResult.StatusCode);
    

    Thanks

提交回复
热议问题