有什么方法可以解决Assert.Istype()失败问题?

时间:2019-05-02 09:30:07

标签: c# unit-testing asp.net-core

当我为test_casedetails_byId失败而写Assert.Istype()

[Fact]
public  void Get_DetailsbyId_NotFound()
{
    //ILoggerMocking

    var serviceProvider = new ServiceCollection()
   .AddLogging()
    .BuildServiceProvider();

    var factory = serviceProvider.GetService<ILoggerFactory>();

    var logger = factory.CreateLogger<DetailsAPIController>();

    //Act
    detailsAPIController = new DetailsAPIController(detailsByIdTestCasePreparation.mockContext.Object, logger);
    //Arrange
    var res = detailsAPIController.GetDetails(22);
    //Assert
    Assert.IsType<NotFoundResult>(res);

}

GetDetailsMethod

[Route("{id}")]
public IActionResult GetDetails(int id)
{
    _logger.LogInformation("Get details API called");

    try
    {
        return Ok(_detailsbyIdRepository.GetDetails(id));

    }
    catch (Exception ex)
    {
        _logger.LogError("An error was thrown because: "+ex.Message);
        return BadRequest(ex);
    }
}

在上面的Assert.IsType实际是失败中,我正在尝试bad_request,但它却得到了OkObjectResult

Expected: Microsoft.AspNetCore.Mvc.NotFoundResult
Actual:  Microsoft.AspNetCore.Mvc.OkObjectResult

任何人都可以帮忙吗

0 个答案:

没有答案