Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I unit test handlers with NServiceBus?
    text
    copied!<p>This should be simple and I'm probably missing something. I'm working from here: <a href="http://support.nservicebus.com/customer/portal/articles/856297-unit-testing" rel="nofollow">http://support.nservicebus.com/customer/portal/articles/856297-unit-testing</a></p> <p>The following always fails:</p> <pre><code>[TestFixture] public class MyTestFixture { [Test] public void RoundTrip() { Test.Initialize(); var correlationId = Guid.NewGuid(); Test.Handler(bus =&gt; new MyHandler(bus)) .ExpectReply&lt;IEventThatHappened&gt;(m =&gt; m.CorrelationId == correlationId) .OnMessage&lt;MyCommand&gt;(m =&gt; new MyCommand(correlationId)); } } public interface IEventThatHappened : IEvent { Guid CorrelationId { get; set; } } public class MyCommand : ICommand { public Guid CorrelationId { get; private set; } public MyCommand(Guid correlationId) { CorrelationId = correlationId; } } public class MyHandler : IHandleMessages&lt;MyCommand&gt; { private readonly IBus _bus; public MyHandler(IBus bus) { if (bus == null) { throw new ArgumentNullException("bus"); } _bus = bus; } public void Handle(MyCommand message) { _bus.Send&lt;IEventThatHappened&gt;(m =&gt; m.CorrelationId = message.CorrelationId); } } </code></pre> <p>If I set a breakpoint inside my handler, the message.CorrelationId == Guid.Empty. The exception thrown during the test is:</p> <p>System.Exception : ExpectedReplyInvocation not fulfilled. Calls made: SendInvocation</p> <p>I've tried using bus.Send, bus.Publish, bus.Reply but each one fails with the corresponding Expected*Invocation . </p> <p>Why is the message.CorrelationId == Guid.Empty instead of the value I supplied? Why doesn't Test.Handler&lt;> detect that I've called Send/Reply/Publish in my handler?</p> <p>NOTE: Using NServiceBus 3.3 from Nuget.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload