2016-05-13 67 views
2

我想弄清楚我在这里错过了什么。我的测试运行良好,但最小起订量VerifyAll正在抛出异常。MOQ错误设置与异步/等待单元测试不匹配

[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; 
    } 
} 

以下设置不匹配...

我不知道这是否是因为异步的方式运行,我的起订量不看嘲笑对象的方法调用?

+0

的是,当不使用安装情况。你设置模拟使用'GetActionPlanReferenceList(“1”)',但叫做'GetActionPlanReferenceList(“1234123”)'。所以根据moq你没有使用设置 – Nkosi

回答

2

安装程序未使用时会发生这种情况。你设置模拟使用GetActionPlanReferenceList("1")但叫GetActionPlanReferenceList("1234123")

所以根据moq你执行的与你设置的不匹配。

你既可以符合预期的参数,或者尝试

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

这将让该方法接受任何字符串奥钢联It.IsAny<string>()表达式参数

+0

欣赏关于It.IsAny +1的额外评论。 – GPGVM