title: How to test with MediatR
body: <p>I've just started out testing <a href="https://github.com/jbogard/MediatR/" rel="noreferrer"><code>MediatR</code></a> to clean up our heavy involved web controllers. Implementing the functionality it self isn't hard at all, but I struggle a little with testing. </p>
  
  <p>I started out with a controller method for canceling a order.</p>
  
  <p>When a order is cancelled, we where  </p>
  
  <ul>
  <li>Setting a 'cancelled' flag on the data in the database</li>
  <li>Logging who cancelled the meeting in an audit log</li>
  <li>Sending a notification by email to some supervisor. </li>
  </ul>
  
  <p>All of this was triggered inside the controller method, making the controller dependent of several different services. When testing, we had to mock each part to isolate the behavior we wanted to test.</p>
  
  <p>The way I implemented this with <code>MediatR</code> was to emit a <code>CancelOrder</code>-query from the controller method. I then made a <code>CancelOrderHandler</code> that is responsible for the storage of the 'cancelled'-flag.</p>
  
  <p>Using a custom <code>MediatorPipeline</code>-implementation, I emit a <code>OrderCancelled</code>-notification if the <code>CancelOrderHandler</code> doesn't throw any exceptions. </p>
  
  <p>I have two handlers for this notification: <code>OrderCancelledAuditLogHandler</code> and <code>OrderCancelledNotificationHandler</code>. The first will take care of the audit log, while the second will send out notifications.</p>
  
  <p>Each handler is easy to test. But how can I test that everything 'fit's together'? I want to make sure that when a order is cancelled, the audit log and notification is actually taken care of. All handlers does things that I really don't want during my test execution (database writes and email sending) and I'm not keen on a full-blown end-to-end integration test. </p>
  
  <p>Any ideas?</p>