Verifying method call with exact arguments with Typemock

526 Views Asked by At

I have a method:

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

And I want to verify, that it was called with right parameters. I've done it all as in 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)
     );
}

But I'm getting exception:

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

Can somebody tell me why I'm getting it?

2

There are 2 best solutions below

0
Eva On BEST ANSWER

Disclaimer, I work in Typemock.

The point is that you're using 'params' keyword. It's wrapping all arguments in one object - array of arguments. So, for proper verification 'params' use the following statement:

 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)
 );

Good luck!

0
Sam On

Have you tried to debug it?

Nevertheless, if you change the last statement to

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

The test will be passed.