Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSubstitute: Checking received methods with array arguments

Tags:

c#

nsubstitute

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?

like image 308
Jimothy Avatar asked Oct 02 '12 22:10

Jimothy


People also ask

How do you check if command execute (); has been received by a substitute?

In some cases (particularly for void methods) it is useful to check that a specific call has been received by a substitute. This can be checked using the Received() extension method, followed by the call being checked. In this case command did receive a call to Execute() , and so will complete successfully.

What is NSubstitute?

NSubstitute is a friendly substitute for . NET mocking libraries. It has a simple, succinct syntax to help developers write clearer tests. NSubstitute is designed for Arrange-Act-Assert (AAA) testing and with Test Driven Development (TDD) in mind.


1 Answers

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

like image 166
aKzenT Avatar answered Oct 12 '22 16:10

aKzenT