Mocking constructor dependencies using AutoMoq attribute
<p>I'm wondering if there is a way of setting up a mock for a dependency before the constructor of the System Under Test (SUT) is called when using a test case that sets up <code>AutoData</code>.</p>

<p>My SUT looks like:</p>

<pre><code>class Sut
{
    private readonly IFoo foo;
    public Sut(IFooFactory factory)
    {
        this.foo = factory.Build(1, 2);
    }

    public IFoo Foo
    {
        get
        {
            return this.foo;
        }
    }
}
</code></pre>

<p>So the test that I'm writing looks like:</p>

<pre><code>[Theory]
[AutoData]
internal void Foo_IsCorrectlySet_Test(
    [Frozen] Mock&lt;IFooFactory&gt; fooFactory,
    IFoo foo,
    Sut sut)
{
    fooFactory.Setup(mock =&gt; mock.Build(1, 2))
        .Returns(foo)
        .Verifiable();

    var actual = sut.Foo;

    Assert.Equal(foo, sut);
    fooFactory.Verify();
}
</code></pre>

<p>Obviously this test fails as the constructor to the <code>Sut</code> runs before I am able to setup the <code>IFooFactory</code>. So I thought that I may have been able to change the declaration of the <code>Sut</code> to <code>Lazy&lt;Sut&gt;</code> in the test.
But again the constructor is still run before the actual test code is run meaning that my test is going to fail.</p>

<p>Now I know that I could easily setup this test with an actual <code>Fixture</code> object and create all of my objects manually and setting them up before I call to create the <code>Sut</code> which is fine but I'm wanting to keep my tests all roughly the same therefore I'm wondering if there is a way that I could still setup my test with the <code>AutoData</code> attribute but not run the constructor until after everything is setup?</p>