title: How do I mock multiple levels of DbSet.Include lambdas?
body: <p>I'm using Moq to write unit tests that use Entity Framework 6 <code>DbSet</code> and <code>DbContext</code> objects.  I have a service method with a cascading/multi-level <code>Include</code> and I can't figure out how to set it up for testing.  The service method looks something like this:</p>
  
  <pre><code>return DataContext.Cars
      .Include(p =&gt; p.Model)
      .Include(p =&gt; p.Model.Make)
      .Select(c =&gt; new 
           {
               Key = c.CarId, 
               Value = string.Format("{0} {1} {2}", c.Model.Make.Name, c.Model.Name, c.Trim)
           }
      ).ToArray();
  </code></pre>
  
  <p>I know that I have to setup the <code>Include</code> to return the mocked object, like this:</p>
  
  <pre><code>mockCarDbSet.Setup(m =&gt; m.Include(It.IsAny&lt;string&gt;())).Returns(mockCarSet.Object);
  </code></pre>
  
  <p>But I'm getting a null reference exception from the cascaded <code>.Include(p =&gt; p.Model.Make)</code>.  How do I set up Moq to handle multiple levels of <code>Include</code>?</p>
  
  <p><strong>EDIT</strong><br>
  OK, so it turns out that I can't use <code>It.IsAny&lt;string&gt;</code> for <code>Include</code> calls that use lambdas instead of strings, so now I have two problems:</p>
  
  <ol>
  <li>How do I setup a mock with Include that accepts a lambda?</li>
  <li>Will the setup for above cascade to multiple levels?</li>
  </ol>