Received() returns error of non-matching call with the same values.

2,118 views
Skip to first unread message

Nathan Middleton

unread,
Jun 5, 2014, 1:13:25 PM6/5/14
to nsubs...@googlegroups.com
I am attempting to verify that this methid is called.
foo.Error(1, null,"", new Exception("Could not find test data"));

I make the method call and through debugger can see the call succeed but when I run the recieved call.
fakefoo.Received().Error(Arg.Is(1), Arg.Is<object>(null), Arg.Is<string>(""), Arg.Is<Exception>(new Exception("Could not find test data")));

The test fails with the following exception.

NSubstitute.Exceptions.ReceivedCallsException: Expected to receive a call matching:
Error(1, <null>, "", System.Exception: Could not find test data)
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
Error(1, <null>, "", *System.Exception: Could not find test data*)

Can anyone explain why System.Exception: Could not find test data does not match System.Exception: Could not find test data

Thanks
N8

Alexandr Nikitin

unread,
Jun 5, 2014, 4:49:48 PM6/5/14
to nsubs...@googlegroups.com
You created two different Exception objects, one in the call and one in the assertion. Exception class is a reference type and compared by reference. 

You can introduce a variable which gives what you want :

var exeption = new Exception("Could not find test data");
foo.Error(1, null,"", exeption);
foo.Received().Error(Arg.Is(1), Arg.Is<object>(null), Arg.Is<string>(""), Arg.Is<Exception>(exeption));
Reply all
Reply to author
Forward
0 new messages