title: Mocking MediatR 3 with Moq
body: <p>We've recently started using MediatR to allow us to de-clutter controller actions as we re-factor a large customer facing portal and convert it all to C#. As part of this we are increasing our unit test coverage as well, but I've hit a problem when trying to mock MediatR itself.</p>
  
  <p>The command does a bunch of stuff to initiate a process and part of this is sending a notification. The notification itself is dealt with by its own handler and therefore would be subject to its own unit test so I want to mock MediatR so that the <code>this.mediator.Send(message)</code> call doesn't really do anything. The handler does return an object but we don't care about it in this context so to all intents and purposes we are treating it as a <code>void</code> return. I just want to verify that <code>Send</code> has been called once as part of the test. However, the <code>Send</code> method is throwing a <code>NullReferenceException</code> and I don't know why.</p>
  
  <p>As of version 3, MediatR now takes a second optional parameter on <code>Send</code>, a <code>CancellationToken</code>, and expression trees require you to explicitly set them so you must specify a value. I've not encountered this before and in my mind I feel that this might be part of the problem but this may be conflation on my part.</p>
  
  <p>Here's a cut down illustration.</p>
  
  <p>SUT </p>
  
  <pre><code>public class TransferHandler : IAsyncRequestHandler&lt;TransferCommand, TransferResult&gt;
  {
      private readonly IMediator mediator;
  
      public TransferHandler(IMediator mediator)
      {
          this.mediator = mediator;
      }
  
      public async Task&lt;TransferResult&gt; Handle(TransferCommand message)
      {
          // Other stuff.
          var notification = new TransferNotificationCommand()
          {
              ClientId = message.clientId,
              OfficeId = message.OfficeId,
              AuthorityFileId = letter?.Id
          };
  
          await this.mediator.Send(notification);    // &lt;=== This is where we get a NullReferenceException, even though nothing is actually null (that I can see).
  
          return new TransferResult()
          {
              Transfer = transfer,
              FileId = letter?.Id
          }
      }
  }
  </code></pre>
  
  <p>Test</p>
  
  <pre><code>public class TransferHandlerTests
  {
      [Theory]
      [AutoData]
      public async void HandlerCreatesTransfer(Mock&lt;IMediator&gt; mockMediator)
      {
          // Note that default(CancellationToken) is the default value of the optional argument.
          mockMediator.Setup(m =&gt; m.Send(It.IsAny&lt;TransferNotificationCommand&gt;(), default(CancellationToken))).Verifiable("Notification was not sent.");
  
          var handler = new TransferHandler(mockMediator.Object);
  
          var actual = await handler.Handle(message);
  
          mockMediator.Verify(x =&gt; x.Send(It.IsAny&lt;CreateIsaTransferNotificationCommand&gt;(), default(CancellationToken)), Times.Once());
      }
  }
  </code></pre>
  
  <p>What am I missing? I feel like I've made a fundamental mistake somewhere but I'm not sure where.</p>