Mocking IEndpointInstance with NSubstitute

110 views
Skip to first unread message

ivanm...@gmail.com

unread,
Oct 4, 2017, 6:32:39 AM10/4/17
to Particular Software
Hi,

In NSB v5 I could mock the IBus and then use methods like received from the mocking lib to test whether the IBus.Send(..) received the expected arguments or not. When I try to mock IEndpointInstance in the same way, I get a System.ArgumentNullException:

Is this behavior by design or not?

And yes, I can use TestableEndpointInstance to achieve the same, and that works. 

I'm using NSubstitute for mocking, and reading the last comment form this SO (https://stackoverflow.com/questions/46226952/how-to-mock-out-iendpointinstance-of-nservicebus#_=_), it says that this can be achieved using Moq, so I wonder why this does not work with NSubstitute.


My NSB v5 test with IBus that works fine:
var ibus = Substitute.For<IBus>();

_myService.DoSomething(objectWithSomeParams); //this one calls IBus.Send(MyMessage)
            
ibus.Received().Send(Arg.Any<MyMessage>());


My NSB v6 test with IEndpointInstance that throws exception:
var endpointInstance = Substitute.For<IEndpointInstance>();

_myService
.DoSomething(objectWithSomeParams); //this one calls IEndpointInstance.Send(MyMessage)
           
endpointInstance
.Received().Send(Arg.Any<MyMessage>());

//throws following exception:
(System.ArgumentNullException : Value cannot be null.
Parameter name: message
   at
NServiceBus.IMessageSessionExtensions.Send(IMessageSession session, Object message) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\IMessageSessionExtensions.cs:line 19)


Current refactor that does the same without mocking
var testableEndpointInstance = new TestableEndpointInstance();

_myService
.DoSomething(objectWithSomeParams); //this one calls IEndpointInstance.Send(MyMessage)

var sentMessages = testableEndpointInstance.SentMessages;
sentMessages
.Length.ShouldBe(1);
sentMessages
[0].Message.ShouldBeOfType<MyMessage>();



Daniel Marbach

unread,
Oct 4, 2017, 6:44:36 AM10/4/17
to Particular Software

Hi ivan,


We recently moved our community platform to https://discuss.particular.net. The Google Group will be locked down very soon. This means there is a chance that you won't get many responses to your question here.


The easiest way to get this question answered is by joining us on https://discuss.particular.net and reposting your question. That way you'll be automatically able to follow any replies.


We apologize for the inconvenience but hope that you will enjoy our new community platform as much as we do!


Best Regards,

Daniel


P.S. For more details about the new discussion group, see our announcement here: https://groups.google.com/forum/#!topic/particularsoftware/gjVrM5radzk.

Reply all
Reply to author
Forward
0 new messages