53

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?

1 Answer 1

81

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

2
  • 1
    After reading your answer, I looked into the NUnit source, and saw that Is.EqualTo (via EqualConstraint) checks collections for equality by comparing items in the collections. That's why capturing the value and checking worked. Now the question is, why don't .Net collections implement Equals, but that's an issue for another day.
    – Jimothy
    Oct 2, 2012 at 22:34
  • Thats a lovely syntax. So much cleaner than RhinoMocks May 24, 2015 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.