title: Unit testing a method that uses (dynamic) using Moq
body: <p>I have the below method:</p>
  
  <pre><code>public void Enqueue(ICommand itemToQueue)
  {
      if (itemToQueue == null)
      {
          throw new ArgumentNullException("itemToQueue");
      }
  
      // Using the dynamic keywork to ensure the type passed in to the generic
      // method is the implementation type; not the interface.
      QueueStorage.AddToQueue((dynamic)itemToQueue);
  }
  </code></pre>
  
  <p>With QueueStorage being a dependency that implements IQueueStorage.  I wish to unit test it but the (dynamic) keyword seems to be blocking Moq from binding correctly to it.  The keyword is used to correctly assign the concrete class type rather than ICommand interface type when it is added to the queue.  </p>
  
  <p>The unit test looks like this:</p>
  
  <pre><code>[Test]
  public void Enqueue_ItemGiven_AddToQueueCalledOnQueueStorage()
  {
      int timesAddToQueueCalled = 0;
  
      var dummyQueueStorage = new Mock&lt;IQueueStorage&gt;();
      var testCommand = new TestCommand();
  
      var queueManager = new AzureCommandQueueManager();
  
      dummyQueueStorage
          .Setup(x =&gt; x.AddToQueue(It.IsAny&lt;TestCommand&gt;()))
          .Callback(() =&gt; timesAddToQueueCalled++);
  
      queueManager.QueueStorage = dummyQueueStorage.Object;
  
      queueManager.Enqueue(testCommand);
  
      Assert.AreEqual(1, timesAddToQueueCalled);
  }
  </code></pre>
  
  <p>Whilst test command is a blank implementation of ICommand:</p>
  
  <pre><code>private class TestCommand : ICommand
  {
  }
  
  public interface ICommand
  {
  }
  </code></pre>
  
  <p>The <code>timesAddedToQueuCalled</code> is not being incremented.  I've tried using <code>It.IsAny&lt;ICommand&gt;</code> and <code>(testCommand)</code> to no avail.  It looks like the Callback method is not being executed.  Can anyone see what I'm doing wrong?</p>
  
  <p>EDIT: IQueueStorage code:</p>
  
  <pre><code>public interface IQueueStorage
  {
      void AddToQueue&lt;T&gt;(T item) where T : class;
  
      T ReadFromQueue&lt;T&gt;() where T : class;
  }
  </code></pre>