使用特定的参数序列多次调用 NSubstitute 方法?
NSubstitute method called multiple times with specific sequence of args?
我想检查某个特定方法是否被调用了 N 次,首先是 arg x1,然后是 x2,然后是 x3,依此类推,最后是 arg xN。我知道可以这样做:
Received.InOrder(() => {
subst.MyMethod(x1);
subst.MyMethod(x2);
subst.MyMethod(x3);
// ...
subst.MyMethod(xN);
});
但是可以通过简单列出参数顺序的方式来完成吗?
类似这样的东西(概念上的):
int[] args = {x1, x2, x3, /*...*/ xN};
subst.Received(N).MyMethod(Arg.Is(args));
这是一个 InOrder
的实现,但我认为它是一种解决方法:
int[] args = {x1, x2, x3, /*...*/ xN};
Received.InOrder(() => {
foreach (int i in args)
subst.MyMethod(i);
});
来自上文:
The NSubstitute API does not have a method for doing this. To me the foreach
method is clearest; it shows exactly what is expected for the test to succeed. You could write a method to package up this logic if you need it frequently, but while it may make it a bit more concise I don't think it will make it any clearer.
我想检查某个特定方法是否被调用了 N 次,首先是 arg x1,然后是 x2,然后是 x3,依此类推,最后是 arg xN。我知道可以这样做:
Received.InOrder(() => {
subst.MyMethod(x1);
subst.MyMethod(x2);
subst.MyMethod(x3);
// ...
subst.MyMethod(xN);
});
但是可以通过简单列出参数顺序的方式来完成吗?
类似这样的东西(概念上的):
int[] args = {x1, x2, x3, /*...*/ xN};
subst.Received(N).MyMethod(Arg.Is(args));
这是一个 InOrder
的实现,但我认为它是一种解决方法:
int[] args = {x1, x2, x3, /*...*/ xN};
Received.InOrder(() => {
foreach (int i in args)
subst.MyMethod(i);
});
来自上文
The NSubstitute API does not have a method for doing this. To me the
foreach
method is clearest; it shows exactly what is expected for the test to succeed. You could write a method to package up this logic if you need it frequently, but while it may make it a bit more concise I don't think it will make it any clearer.