C# MOQ错误设置与异步/等待单元测试不匹配
我正试图找出我在这里遗漏了什么。我的测试运行正常,但我的MOQC# MOQ错误设置与异步/等待单元测试不匹配,c#,unit-testing,asynchronous,moq,C#,Unit Testing,Asynchronous,Moq,我正试图找出我在这里遗漏了什么。我的测试运行正常,但我的MOQVerifyAll引发异常 [TestMethod] public async Task ActionPlanDataProvider_GetActionPlanReferenceList_ReturnsValid() { try { //Arrange Mock<IActionPlanDataProvider> moqAPlan = new Mock<IActionP
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;
}
}
[TestMethod]
公共异步任务ActionPlanDataProvider\u GetActionPlanReferenceList\u ReturnsValid()
{
尝试
{
//安排
Mock moqAPlan=新Mock();
//Setup(x=>x.GetActionPlanReferenceList()).ReturnsAsync(新的ActionPlanReferenceList());
莫卡普兰
.Setup(x=>x.GetActionPlanReferenceList(“1”))
.Returns(Task.FromResult(新的ActionPlanReferenceList());
//表演
var d=await moqAPlan.Object.GetActionPlanReferenceList(“1234123”);
//断言
moqAPlan.VerifyAll();
}
捕获(例外情况除外)
{
字符串a=例如消息;
投掷;
}
}
以下设置不匹配
我想知道这是否是因为异步运行的方式使我的MOQ没有看到模拟对象方法调用?当不使用设置时会发生这种情况。您将模拟设置为使用
GetActionPlanReferenceList(“1”)
,但调用了GetActionPlanReferenceList(“1234123”)
所以根据moq,你执行的与你设置的不匹配
您可以匹配预期参数,也可以尝试
moqAPlan
.Setup(x => x.GetActionPlanReferenceList(It.IsAny<string>()))
.Returns(Task.FromResult(new ActionPlanReferenceList()));
moqAPlan
.Setup(x=>x.GetActionPlanReferenceList(It.IsAny()))
.Returns(Task.FromResult(新的ActionPlanReferenceList());
这将允许方法接受未使用设置时发生的
It.IsAny()
expression参数中的任何字符串。您将模拟设置为使用GetActionPlanReferenceList(“1”)
,但调用了GetActionPlanReferenceList(“1234123”)
。因此,根据moq,您没有使用设置。请欣赏关于它的附加评论。IsAny+1