如何验证将特定方法传递给模拟方法

时间:2019-02-06 12:09:33

标签: .net moq

我的待测课程中有以下方法。

public Task MethodToTest(string p)
{
   await dependency.SomeMethodAsync(() => dependency.AnotherMethodAsync(p));
}

当前,我正在尝试是否有可能验证dependency.SomeMethodAsync是否实际上以dependency.AnotherMethodAsync作为参数被调用。

我设法很好地模拟了测试中的第一种方法。

mockedDependency.Setup(d => d.SomeMethodAsync(It.IsAny<Func<Task>>())).ReturnsAsync(...);

var response = await myClass.MethodToTest(string.Empty);

但是,我没有找到一种方法(如果可能)来验证AnotherMethod调用。 以下编译,但抛出 NotSupportedException:不支持的表达式

mockedDependency.Verify(s => s.SomeMethodAsync(() => s.AnotherMethodAsync(string.Empty)), Times.Once);

关于如何(以及是否)可以验证的任何想法?

1 个答案:

答案 0 :(得分:0)

替换 mockedDependency.Verify(s => s.SomeMethodAsync(() => s.AnotherMethodAsync(string.Empty)), Times.Once);

有两行:

var v = mockedDependency.Object.AnotherMethodAsync(string.Empty);
mockedDependency.Verify(s => s.SomeMethodAsync(v), Times.Once);

也许,您将不得不为AnotherMethodAsync设置一个模拟。