【问题标题】:MOQ error setups not matched with Async / Await Unit TestMOQ 错误设置与异步/等待单元测试不匹配
【发布时间】:2016-05-13 16:17:53
【问题描述】:

我想弄清楚我在这里缺少什么。我的测试运行良好,但我的 MOQ 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;
    }
}

以下设置不匹配...

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

【问题讨论】:

  • 不使用安装程序时会发生这种情况。您将模拟设置为使用GetActionPlanReferenceList("1"),但称为GetActionPlanReferenceList("1234123")。所以根据起订量你没有使用设置

标签: c# unit-testing asynchronous moq


【解决方案1】:

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

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

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

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

这将让该方法接受任何字符串而不是It.IsAny&lt;string&gt;() 表达式参数

【讨论】:

  • 感谢关于 It.IsAny +1 的附加评论
猜你喜欢
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2018-02-03
  • 2014-05-21
相关资源
最近更新 更多