title: Creating a Mock For a Deeply Nested Class Using AutoFixture/AutoMoq?
body: <p>I'd like to write a unit test that overrides some readonly property that is fairly deep in an object graph.  What I mean is a method like this:</p>
  
  <pre><code>public string MethodToTest(IClassA classA)
  {
      return classA.ClassB.ClassC.ClassD.Items[0].Name;
  }
  </code></pre>
  
  <p>Where each ClassN implements an interface IClassN and each property is readonly.  So an example interface would be:</p>
  
  <p><strong>IClassA</strong></p>
  
  <pre><code>public interface IClassA { IClassB ClassB { get; } }
  </code></pre>
  
  <p>And the implementation would look like:</p>
  
  <p><strong>ClassA</strong></p>
  
  <pre><code>public class ClassA : IClassA
  {
      public ClassA() { ClassB = new ClassB(); }
      public IClassB ClassB { get; }
  }
  </code></pre>
  
  <p>I want to override the value returned by <em>classA.ClassB.ClassC.ClassD.Items[0].Name</em>, with as little effort as possible.  I could create a Mock and have a .Setup to return an IClassB and go up the entire chain just using Moq.  But I'd like to avoid that if possible.</p>
  
  <p>I've tried a lot of different things, but have had no luck.  </p>
  
  <p><strong>Attempt #1</strong></p>
  
  <p>I thought I could create the chain using <code>fixture.Build()</code></p>
  
  <pre><code>var moqItem = new Mock&lt;IItem&gt;();
  moqItem.Setup(item =&gt; item.Name).Returns("My expected value");
  var fakeClassD = fixture.Build&lt;IClassD&gt;()
                          .With(d =&gt; d.Items, new[] { moqItem.Object });
  </code></pre>
  
  <p>Obviously I've left off some layers, but it doesn't matter.  This fails because the properties are readonly.</p>
  
  <p><strong>Attempt #2</strong></p>
  
  <p>Next I thought I could 'freeze' a particular instance, and whenever the fixture was creating an object, if it saw something of that type, it would use it.  I thought I was following the example given here:  <a href="https://blog.ploeh.dk/2010/03/17/AutoFixtureFreeze/" rel="nofollow noreferrer">https://blog.ploeh.dk/2010/03/17/AutoFixtureFreeze/</a></p>
  
  <p>It shows this like of code: <code>var expectedName = fixture.Freeze("Name");</code></p>
  
  <p>Based on that, I tried to do something like this:</p>
  
  <pre><code>var moqItem = new Mock&lt;IItem&gt;();
  moqItem.Setup(x =&gt; x.Name).Returns("My expected value");
  fixture.Freeze&lt;IItem[]&gt;(new IItem[] { moqItem.Object });
  </code></pre>
  
  <p>Sadly, that won't even compile.  The Freeze method is expecting a Func of some Composer class of type IItem[] and I haven't been able to figure out how to do that.  If I remove the type, similar to the sample code I get</p>
  
  <pre><code>fixture.Freeze(new IItem[] { moqItem.Object });
  </code></pre>
  
  <p>Which also fails to compile.</p>
  
  <p><strong>Attempt #3</strong></p>
  
  <pre><code>var moqItem = new Mock&lt;IItem&gt;();
  moqItem.Setup(x =&gt; x.Name).Returns("My expected value");
  fixture.Inject&lt;IItem[]&gt;(new IItem[] { moqItem.Object });
  </code></pre>
  
  <p>Very similar to attempt #2 - only this does compile.  I thought that whenever the fixture needed an IItem[] array, it would use the one I setup.  But when I call </p>
  
  <pre><code>var attempt3 = fixture.Create&lt;IClassA&gt;();
  </code></pre>
  
  <p>The behavior isn't what I hoped.  attempt3.ClassB.ClassC.ClassD.Items does not contain my mocked item.</p>
  
  <p>TL;DR - How can I override the value returned from <code>Item[0].Name</code> with the least amount of code/effort?  </p>