使用 Typemock 验证带有精确参数的方法调用

Verifying method call with exact arguments with Typemock

我有一个方法:

 public bool Foo(params Object[] list)
 {
     //Some manipulations with list
     return true;
 }

而且我想验证它是使用正确的参数调用的。我已经完成了 docs:

[TestMethod, Isolated]
public void TestArgs()
{
    var bar = new Bar();

    Isolate.WhenCalled(() => bar.Foo()).CallOriginal();

    string arg1 = "Apple";
    string arg2 = "Pear";
    string arg3 = "Potato";
    bar.Foo(arg1, arg2, arg3);

    Isolate.Verify.WasCalledWithArguments(() => bar.Foo(null, null, null)).Matching(a =>
        (a[0] as string).Equals(arg1) &&
        (a[1] as string).Equals(arg2) &&
        (a[2] as string).Equals(arg3)
     );
}

但我遇到异常:

System.NullReferenceException : Object reference not set to an instance of an object.

有人能告诉我为什么我会得到它吗?

你试过调试吗?

不过,如果将最后一条语句更改为

Isolate.Verify.WasCalledWithExactArguments(() => bar.Foo(arg1, arg2, arg3));

考试会通过

免责声明,我在 Typemock 工作。

重点是您使用的是 'params' 关键字。它将所有参数包装在一个对象中——参数数组。因此,为了正确验证 'params' 使用以下语句:

 Isolate.Verify.WasCalledWithArguments(() => bar.Foo(null)).Matching(a =>
     (a[0] as string[])[0].Equals(arg1) &&
     (a[0] as string[])[1].Equals(arg2) &&
     (a[0] as string[])[2].Equals(arg3)
 );

祝你好运!