title: How to call constructor with interface arguments when mocking a concrete class with Moq
body: <p>I have the following class, which uses constructor injection:</p>
  
  <pre><code>public class Service : IService
  {
      public Service(IRepository repository, IProvider provider) { ... }
  }
  </code></pre>
  
  <p>For most methods in this class, I simply create Moq mocks for <code>IRepository</code> and <code>IProvider</code> and construct the <code>Service</code>. However, there is one method in the class that calls several other methods in the same class. For testing this method, instead of testing all those methods together, I want to test that the method calls those methods correctly and processes their return values correctly.</p>
  
  <p>The best way to do this is to mock <code>Service</code>. I've mocked concrete classes with Moq before without issue. I've even mocked concrete classes that require constructor arguments with Moq without issue. However, this is the first time I've needed to pass <strong>mocked</strong> arguments into the constructor for a mocked object. Naturally, I tried to do it this way:</p>
  
  <pre><code>var repository = new Mock&lt;IRepository&gt;();
  var provider = new Mock&lt;IProvider&gt;();
  
  var service = new Mock&lt;Service&gt;(repository.Object, provider.Object);
  </code></pre>
  
  <p>However, that does not work. Instead, I get the following error:</p>
  
  <pre><code>Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: My.Namespace.Service.
  Could not find a constructor that would match given arguments:
      Castle.Proxies.IRepository
      Castle.Proxies.IProvider
  </code></pre>
  
  <p>This works fine if <code>Service</code>'s constructor takes simple arguments like <code>int</code>s and <code>string</code>s, but not if it takes interfaces that I'm mocking. How do you do this?</p>