title: Moq - Correct setup for mocking generic method
body: <p>I am trying to mock a generic method and it doesn't work as expected.</p>
  
  <p>I have this service definition</p>
  
  <pre><code>public interface ICommandHandlerFactory {
      ICommandHandler&lt;T&gt; GetHandler&lt;T&gt;() where T : ICommand;
  }
  </code></pre>
  
  <p>and this Moq setup</p>
  
  <pre><code>var handler = new TestCommandHandler();
  var handlerFactory = Mock.Of&lt;ICommandHandlerFactory&gt;(o =&gt; 
       o.GetHandler&lt;TestCommand&gt;() == handler);
  </code></pre>
  
  <p>If I call <code>GetHandler</code> method on the mock with the specific type e.g. <code>GetHandler&lt;TestCommand&gt;</code> everything is working as expected and it returns instance of the <code>TestCommandHandler</code> class.</p>
  
  <p>But if the mock is injected to another generic class</p>
  
  <pre><code>public class CommandBus {
      private ICommandHandlerFactory _handlerFactory;
  
      public ICommandHandler GetHandler&lt;T&gt;(T command) where T : ICommand {
          return _handlerFactory.GetHandler&lt;T&gt;();
      }
  }
  </code></pre>
  
  <p>the following piece of code return <code>null</code></p>
  
  <pre><code>var command = new TestCommand();
  return commandBus.GetHandler(command);
  </code></pre>
  
  <p>How should I setup Moq to return correct handler even in this case?</p>