title: Mock Linq `Any` predicate with Moq
body: <p>I'm trying to mock <code>AnyAsync</code> method in my repository with below codes but repository always returns <code>false</code>.</p>
  
  <p>The signature of <code>AnyAsync</code> is:</p>
  
  <pre><code>Task&lt;bool&gt; AnyAsync&lt;TEntity&gt;(Expression&lt;Func&lt;TEntiry, bool&gt;&gt; predicate)
  </code></pre>
  
  <p>I tried the following setups:</p>
  
  <p>1:</p>
  
  <pre><code>mockCustomerRepository.Setup(r =&gt; r.AnyAsync(c =&gt; c.Email == "some@one.com"))
     .ReturnsAsync(true);
  </code></pre>
  
  <p>2:</p>
  
  <pre><code>Expression&lt;Func&lt;CustomerEntity, bool&gt;&gt; predicate = expr =&gt; 
      expr.CustomerPerson.Email == "some@one.com";
  
  mockCustomerRepository.Setup(r =&gt; r.AnyAsync(It.Is&lt;Expression&lt;Func&lt;CustomerEntity, bool&gt;&gt;&gt;
     (criteria =&gt; criteria == predicate))).ReturnsAsync(true);
  </code></pre>
  
  <p>3:</p>
  
  <pre><code>mockCustomerRepository.Setup(r =&gt; r.AnyAsync(It.IsAny&lt;Expression&lt;Func&lt;CustomerEntity, bool&gt;&gt;&gt;()))
      .ReturnsAsync(true);
  </code></pre>
  
  <p>My test:</p>
  
  <pre><code>public class Test 
  {
      Mock&lt;ICustomerRepository&gt; mockCustomerRepository;
  
      public Test()
      {
          mockCustomerRepository = new Mock&lt;ICustomerRepository&gt;();
      }
  
      [Fact]
      public async Task CustomerTest()
      {   
          var customer = ObjectFactory.CreateCustomer(email: "some@one.com");
          var sut = new CustomerService(mockCustomerRepository.Object);
  
          var result = await sut.ValidateCustomerAsync(customer);
          .
          .
          .
      }
  }
  </code></pre>
  
  <p>My <code>CustomerService.ValidateCustomerAsync</code> method:</p>
  
  <pre><code>public async Task&lt;OperationResult&gt; ValidateCustomerAsync(CustomerEntity customer)
  {
      var errors = new List&lt;ValidationResult&gt;();
  
      if (await _repository.AnyAsync(c =&gt; c.Email == customer.Email))
          errors.Add(new ValidationResult("blah blah")));
  </code></pre>
  
  <p>I've also read <a href="https://stackoverflow.com/questions/6835366/moq-linq-predicates-in-setup-method">this</a> but it doesn't work too.</p>