无法在 Xunit 中断言 returns jsonresult 的 ActionResult

Unable to assert ActionResult which returns jsonresult in Xunit

我正在尝试声明一个 IActionResult,其中 returns 一个 JsonResult。我无法访问 IActionResult 值。

我该怎么做?

更新

这是代码。

[Fact]
    public async void AppendEmailTemplateUrl_ReturnOk()
    {
        // Arrange
        var mockLogger = new Mock<ILogger<EmailController>>();
        var mockMapper = new Mock<IMapper>();
        var mockEmsWorkUnit = new Mock<IEmsWorkUnit>();
        var mockAzureBlob = new Mock<IAzureBlob>();

        // Setup
        var userRequestTemplateString = File.ReadAllText(@".\EmailController\UserRequestTemplate.txt");
        mockAzureBlob.Setup(blob => blob.GetHtmlBlob(It.IsAny<string>(), It.IsAny<Uri>()))
            .ReturnsAsync(userRequestTemplateString);

        var emailController = new EmailController(mockLogger.Object, mockMapper.Object, mockEmsWorkUnit.Object, mockAzureBlob.Object);
        var jsonString = File.ReadAllText(@".\EmailController\TemplateUrlOk.json");
        var testEmailUrlDto = GeneralHelpers.Deserialize<EmailUrlDto>(jsonString);

        // Act
        var result = await emailController.AppendEmailTemplateUrl(testEmailUrlDto);

        // Assert

    }

JsonResult.Value 转换回要格式化的已知类型并对其执行断言

//...omitted for brevity

// Act
IActionResult result = await emailController.AppendEmailTemplateUrl(testEmailUrlDto);

//Assert
var actual = result as JsonResult;
Assert.IsNotNull(actual);
var model = actual.Value as ApiResult;
Assert.IsNotNull(model);

//...assert model

同时重构测试以使用 async Task 而不是 async void

[Fact]
public async Task AppendEmailTemplateUrl_ReturnOk() {
    //...omitted for brevity
}