我正在开发一个.Net Core Web API应用程序。我在控制器上有以下方法:
[HttpGet]
public async Task<IActionResult> GetArtists()
{
var permissions = await _permissionsService.GetPermissionsAsync(HttpContext);
var artists = _artistsService.GetAllArtists(permissions.UserId, permissions.IsAdministrator);
return Ok( new { artists });
}
我希望编写一个测试来验证我确实获得了200 OK结果。
我尝试了以下方法:
[TestMethod]
public void GetArtists_ReturnsOKStatusCode()
{
// arrange
var artistsController = new ArtistsController(_mockPermissionsService.Object, _mockArtistsService.Object, _mockLogger.Object);
// act
var getArtistsResult = artistsController.GetArtists();
var okResult = getArtistsResult as OkObjectResult;
Assert.IsInstanceOfType(okResult, OkObjectResult)
}
但是在进行转换为OkObjectResult
的那一行时,我遇到了错误。它说我无法将类型为Task<IActionResult>
的内容转换为OkObjectResult
。