NSubstitute: Checking received methods with array arguments

Jimothy picture Jimothy · Oct 3, 2012 · Viewed 17.1k times · Source

I want to verify that a method on my NSubstitute mock is called with a particular array argument.

Say the interface, IProcessor, has a method void ProcessSomething(Foo[] something]). Say my class under test is named Commander. I set up my test like this:

//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

The Received() call fails with:

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[]*)

So this looks like ProcessSomething was called with some array other than foos, right?

Well, if I instead test this like, where I capture the argument value using Arg.Do(), it succeeds:

//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

So capturing the argument and comparing it for equality (with NUnit in this example) works, but verifying the received call fails.

Is this a bug in NSubstitute, or am I using it wrong?

Answer

aKzenT picture aKzenT · Oct 3, 2012

I assume that your Commander object will take the arguments and puts them in an array which it then uses to call the Processor mock.

Your foos variable is another array which you create on your setup. Arrays don't compare equal to each other even if they have the same elements. So NSubstitute will complain that it didn't receive the expected value (it received another array which happened to contain the same elements).

Edit: Try this version:

//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(Arg.Is<Foo[]>(foos2 => foos.SequenceEqual(foos2));

This requires importing the System.Linq Namespace