title: Moq: Lambda expressions as parameters and evaluate them in returns
body: <p>In my unit tests I want to be able to moq the "find" function of my repository in my Unit of Work that takes in a lambda express. For example:</p>
  
  <pre><code>public virtual IQueryable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
  { 
      // Find OPS 
  }
  </code></pre>
  
  <p>I created test lists to represent my dbSets in my unit tests (Also I left out a lot of abstraction to make this more simple for this question):</p>
  
  <pre><code>[TestMethod]
  public void FindTest()
  {
      var mockUnitOfWork = new Mock&lt;IUnitOfWork&gt;();
      var testList = new List&lt;ListObject&gt; 
      { 
          // Test values 
      }
      // Here is where I am stuck:
      mockUnitOfWork.Setup(uow =&gt; uow.Find(It.IsAny&lt;Expression&lt;Func&lt;ListObject, bool&gt;&gt;&gt;()))
                                     .Returns(/* ???? testList.Where(??????) ???? */);
  }
  </code></pre>
  
  <p>I want to be able to use the lambda passed by the method calling the mock's find to search within my mocked list. Is this possible? </p>
  
  <p><strong>EDIT:</strong> Thanks to Chris for the answer. This is setup code to pass the lambda expression that is passed by the method that references the mock's function:</p>
  
  <pre><code>mockUnitOfWork
  .Setup(uow =&gt; uow.Find(It.IsAny&lt;Expression&lt;Func&lt;ListObject, bool&gt;&gt;&gt;()))
  .Returns(new Func&lt;Expression&lt;Func&lt;ListObject, bool&gt;&gt;, IQueryable&lt;ListObject&gt;&gt;(
      expr =&gt; testList.Where(expr.Compile()).AsQueryable()));
  </code></pre>