Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to verify multiple actions in NServiceBus Handler
    text
    copied!<p>I have a handler that needs to send a local message on every instance of an entity retrieved from a repository. The callback passed to <code>NServiceBus.Testing.Handler&lt;&gt;::ExpectSendLocal()</code> only gets invoked on the first invocation of <code>IBus::SendLocal()</code> in my handler. I've tried chaining a second <code>Handler&lt;&gt;::ExpectSendLocal()</code> but its callback is only invoked on the first one too. Here's an example handler:</p> <pre><code>public FooHandler : IHandleMessages&lt;IFoo&gt; { public void Handle(IFoo message) { var bars = new [] {new Bar {Zap = "Zap1"}, new Bar {Zap = "Zap2"}}; foreach (var bar in bars) { this.Bus().SendLocal&lt;IBarProcessed&gt;(barMsg =&gt; { barMsg.Zap = bar.Zap; }); bar.IsProcessed = true; } } } </code></pre> <p>Here's an example unit test that increments a count for <code>IBus::SendLocal()</code> expectations:</p> <pre><code>public void When_IFoo_message_received() { int actualCount = 0; new NServiceBus.Testing.Handler&lt;FooHandler&gt;() .ExpectSendLocal&lt;IBarProcessed&gt;(completed =&gt; actualCount++) .OnMessage&lt;IFoo&gt;(); Assert.AreEqual(2, actualCount); // fails because actualCount is one } </code></pre> <p>Here's an example unit test that chains <code>NServiceBus.Testing.Handler&lt;FooHandler&gt;.ExpectSendLocal()</code> and checks for 2 different values from 2 messages:</p> <pre><code>public void When_IFoo_message_received() { new NServiceBus.Testing.Handler&lt;FooHandler&gt;() .ExpectSendLocal&lt;IBarProcessed&gt;(completed =&gt; { Assert.AreEqual("Zap1", completed.Zap); }) .ExpectSendLocal&lt;IBarProcessed&gt;(completed =&gt; { Assert.AreEqual("Zap2", completed.Zap); // fails because it's the value from the first one e.g. "Zap1" }) .OnMessage&lt;IFoo&gt;(); } </code></pre>
 

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