我想验证我的 NSubstitute 模拟上的方法是否使用特定的数组参数调用。
说接口, IProcessor
, 有一个方法void ProcessSomething(Foo[] something])
。假设我的测试类名为Commander
. 我这样设置我的测试:
//prepare
var processor = Substitute.For<IProcessor>;
var commander = new Commander(processor);
var foo1 = new Foo("alpha");
var foo2 = new Foo("bravo");
var foos = new [] {foo1, foo2};
//act
commander.DoSomething(foo1, foo2);
//verify
processor.Received().ProcessSomething(foos); // FAILS
Received()
调用失败并显示:
NSubstitute.Exceptions.ReceivedCallsException : Expected to receive a call matching:
ProcessSomething(Foo[])
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
ProcessSomething(*Foo[]*)
所以这看起来像 ProcessSomething 是用其他数组调用的foos
,对吧?
好吧,如果我改为测试这个,我使用 捕获参数值Arg.Do()
,它会成功:
//prepare
//... as before
var actualFoos = null;
processor.ProcessSomething(Arg.Do<Foo[]>(x => actualFoos = x));
//act
commander.DoSomething(foo1, foo2);
//verify
Assert.That(actualFoos, Is.EqualTo(foos)); // SUCCEEDS
因此,捕获参数并比较它是否相等(在此示例中使用 NUnit)是可行的,但验证接收到的调用失败。
这是 NSubstitute 中的错误,还是我用错了?