title: How to mock chained Linq methods in Moq
body: <p>I have a query that goes</p>
  
  <pre><code>var myRecord = myRepository.All().Any(e =&gt; e.Id == id);
  </code></pre>
  
  <p>I mocked it with this</p>
  
  <pre><code>this._myMockRepository
      .Setup(s =&gt; s.All().Any(It.IsAny&lt;Expression&lt;Func&lt;MyObject, bool&gt;&gt;&gt;()))
      .Returns(this.GetFakeMyObjects().Any(t =&gt; t.Id == 1));
  </code></pre>
  
  <p>How do I mock the <code>.All().Any(...)</code> call? The exception I get is a <code>NotSupportedException</code> with message :</p>
  
  <blockquote>
    <p>Expression references a method that does not belong to the mocked object: s => s.All().Any &lt; MyRepositoryObject > (It.IsAny &lt; Expression`1 > ())</p>
  </blockquote>
  
  <p>I tried breaking it up like so </p>
  
  <pre><code>this._myMockRepository
      .Setup(s =&gt; s.All(It.IsAny&lt;Expression&lt;Func&lt;MyObject, bool&gt;&gt;&gt;()))
      .Returns(this.GetFakeMyObjects());
  this._myMockRepository
      .Setup(s =&gt; s.All().Any(It.IsAny&lt;Expression&lt;Func&lt;MyObject, bool&gt;&gt;&gt;()))
      .Returns(this.GetFakeMyObjects().Any(t =&gt; t.Id == 1));
  </code></pre>
  
  <p>Fairly new to Moq so any tips would be appreciated.</p>