使用参数调用 moq 方法验证一次
verify with moq method is called with argument once
被测class(A)调用了对象b的一个方法。我想验证调用的方法是否针对每个定义的参数组合执行一次。
对于以下代码,我想验证 CalledMethod 恰好一次。目前我验证参数在参数列表中。但是如何验证参数是否适合并只使用一次?
public void CalledMethod(string source, string target);
List<string[]> paths = new()
{
new string[] { "incomingPath1", "targetPath1"},
new string[] { "incomingPath2", "targetPath2" },
new string[] { "incomingPath3", "targetPath3" },
};
// rearrange arguments
List<string> incomingPaths = ...
List<string> targetPaths = ...
// current verification codecode
mock.Setup(foo => foo.CalledMethod(It.IsIn<String>(incomingPaths), It.IsIn<String>(targetPaths), true));
// do stuff
...
// verify method is called once with each argument pair
// How?
只需使用每个确切的参数对调用 Verify
:
foreach (var path in paths) {
mock.Verify(x => x.CalledMethod(path[0], path[1]), Times.Once());
}
被测class(A)调用了对象b的一个方法。我想验证调用的方法是否针对每个定义的参数组合执行一次。
对于以下代码,我想验证 CalledMethod 恰好一次。目前我验证参数在参数列表中。但是如何验证参数是否适合并只使用一次?
public void CalledMethod(string source, string target);
List<string[]> paths = new()
{
new string[] { "incomingPath1", "targetPath1"},
new string[] { "incomingPath2", "targetPath2" },
new string[] { "incomingPath3", "targetPath3" },
};
// rearrange arguments
List<string> incomingPaths = ...
List<string> targetPaths = ...
// current verification codecode
mock.Setup(foo => foo.CalledMethod(It.IsIn<String>(incomingPaths), It.IsIn<String>(targetPaths), true));
// do stuff
...
// verify method is called once with each argument pair
// How?
只需使用每个确切的参数对调用 Verify
:
foreach (var path in paths) {
mock.Verify(x => x.CalledMethod(path[0], path[1]), Times.Once());
}