Mock nested dependencies with MOQ
<p>I assumed that MOQ would automatically create Mocks for any nested dependencies.</p>

<p>I am unit testing an ASP.Net MVC Controller:</p>

<pre><code>public class TransactionController : Controller
{
    private readonly ITransactionService _transactionService;
    private readonly SearchPanelVmBuilder _searchPanelVmBuilder;
    private readonly TransactionVmsBuilder _transactionVmsBuilder;

    public TransactionController(TransactionVmsBuilder transactionVmsBuilder, ITransactionService transactionService, SearchPanelVmBuilder searchPanelVmBuilder)
    {
        _transactionVmsBuilder = transactionVmsBuilder;
        _transactionService = transactionService;
        _searchPanelVmBuilder = searchPanelVmBuilder;
    }

    // other methods omitted for brevity

    public PartialViewResult SearchPanel()
    {
        var vm = _searchPanelVmBuilder.BuildVm();

        return PartialView("_SearchPanel", vm);
    }
}
</code></pre>

<p>The unit test code:</p>

<pre><code>[Fact]
public void SeachPanel_Calls_BuildSearchPanelVm()
{
    // Arrange
    var mockTransService = new Mock&lt;ITransactionService&gt;();
    var mockTransVmsBuilder = new Mock&lt;TransactionVmsBuilder&gt;();
    var mockSearchPanelVmBuilder = new Mock&lt;SearchPanelVmBuilder&gt;();
    var controller = new TransactionController(mockTransVmsBuilder.Object, mockTransService.Object, mockSearchPanelVmBuilder.Object);

    // Act
    controller.SearchPanel();

    // Assert
    mockSearchPanelVmBuilder.Verify(x =&gt; x.BuildVm());
}
</code></pre>

<p>MOQ complains: </p>

<blockquote>
  <p>Can not instantiate proxy of class: MCIP.Web.UI.ViewModelBuilders.Singular.SearchPanelVmBuilder.
  Could not find a parameterless constructor.</p>
</blockquote>

<p>The class it can't instantiate a proxy for:</p>

<pre><code>public class SearchPanelVmBuilder
{
    private readonly ITransactionTypeService _transactionTypeService;
    private readonly TransactionTypeVmBuilder _transactionTypeVmBuilder;
    private readonly UserProvider _userProvider;

    public SearchPanelVmBuilder(
        UserProvider userProvider,
        ITransactionTypeService transactionTypeService,
        TransactionTypeVmBuilder transactionTypeVmBuilder
        )
    {
        _userProvider = userProvider;
        _transactionTypeService = transactionTypeService;
        _transactionTypeVmBuilder = transactionTypeVmBuilder;
    }

    public virtual SearchPanelVm BuildVm()
    {
        return new SearchPanelVm
        {
            Userlist = _userProvider.GetOperators(),
            TransactionTypes =
                _transactionTypeService.GetAll().Select(x =&gt; _transactionTypeVmBuilder.BuildVmFromModel(x)).ToList()
        };
    }
}
</code></pre>

<p>Its corresponding dependencies:</p>

<pre><code>public class UserProvider
{
    private static int retryCount;

    public virtual List&lt;string&gt; GetOperators()...

    public virtual List&lt;string&gt; GetGroupsForUser(WindowsIdentity identity)...
}

public interface ITransactionTypeService
{
    List&lt;TransactionType&gt; GetAll();
}

public class TransactionTypeVmBuilder
{
    public virtual TransactionTypeVm BuildVmFromModel(TransactionType transactionType)...
}
</code></pre>

<p>Am I doing something wrong?</p>

<p>Do I have to explicitly tell MOQ to have a go at auto-mocking nested dependencies?</p>

<p>Or do i have to explicitly set up the nested Mocks - like this:</p>

<pre><code>var mockUserProvider = new Mock&lt;UserProvider&gt;();
var mockTransTypeService = new Mock&lt;ITransactionTypeService&gt;();
var mockTransactionTypeVmBuilder = new Mock&lt;TransactionTypeVmBuilder&gt;();
var mockSearchPanelVmBuilder = new Mock&lt;SearchPanelVmBuilder&gt;(mockUserProvider.Object, mockTransTypeService.Object, mockTransactionTypeVmBuilder.Object);
</code></pre>