c a n d l a n d . n e t

Unit Testing NServiceBus Send Methods

Dusty Candland | |
Here’s a static method for helping test the Send calls when using NService Bus. It depends on RhinoMocks.
 

public static class BusExpectationExtensions

{

    public static IBus ExpectSend(this IBus bus, IMessage firstMessage, IMessage returnMessage)

    {

        var callback = MockRepository.GenerateStub<ICallback>();

 

        bus.Expect(b => b.Send(firstMessage)).IgnoreArguments().Return(callback);

 

        callback.Expect(c => c.Register(null, null)).IgnoreArguments()

            .Return(new BusAsyncResult(null, null))

            .WhenCalled(invocation =>

            {

                var ar = new BusAsyncResult(invocation.Arguments[0] as AsyncCallback, invocation.Arguments[1]);

                ar.Complete(0, returnMessage);

                invocation.ReturnValue = ar;

            });

        return bus;

    }

}

To use it, create a stub of IBus, and then call ExpectSend with your send and return messages like;

var bus = MockRepository.GenerateStub<IBus>()

    .ExpectSend(new SendMessage(2, 22), new MessageReturned(2, 22, 3.00m))

    .ExpectSend(new SendMessage(4, 44), new MessageReturned(4, 44, 234.34m));

 

var filter = new SomeFilteringClass(bus);

 

… execute and assert

Webmentions

These are webmentions via the IndieWeb and webmention.io. Mention this post from your site: