最小起订量错误设置与异步/等待单元测试不匹配

MOQ error setups not matched with Async / Await Unit Test

我想弄清楚我在这里遗漏了什么。我的测试运行正常,但我的最小起订量 VerifyAll 抛出异常。

[TestMethod]
public async Task ActionPlanDataProvider_GetActionPlanReferenceList_ReturnsValid()
{
    try
    {
        //Arrange
        Mock<IActionPlanDataProvider> moqAPlan = new Mock<IActionPlanDataProvider>();
        //moqAPlan.Setup(x => x.GetActionPlanReferenceList()).ReturnsAsync(new ActionPlanReferenceList());
        moqAPlan
            .Setup(x => x.GetActionPlanReferenceList("1"))
            .Returns(Task.FromResult(new ActionPlanReferenceList()));

        //Act
        var d = await moqAPlan.Object.GetActionPlanReferenceList("1234123");

        //Assert
        moqAPlan.VerifyAll();
    }
    catch (Exception ex)
    {
        string a = ex.Message;
        throw;
    }
}

The following setups were not matched...

我想知道这是否是因为我的 MOQ 没有看到模拟对象方法调用的异步运行方式?

未使用安装程序时会发生这种情况。您将模拟设置为使用 GetActionPlanReferenceList("1") 但调用了 GetActionPlanReferenceList("1234123")

所以根据最小起订量,您执行的与您设置的不匹配。

您可以匹配预期参数或尝试

moqAPlan
    .Setup(x => x.GetActionPlanReferenceList(It.IsAny<string>()))
    .Returns(Task.FromResult(new ActionPlanReferenceList()));

这将使该方法通过 It.IsAny<string>() 表达式参数接受任何字符串