当我为test_case
写details_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
任何人都可以帮忙吗