title: Moq repository setup of the FindBy method with Func<>
body: <p>Im new moq and have been struggling with the following. </p>
  
  <p>I have mocked a list of List called _mockedThingsList</p>
  
  <p>I would like to Moq the FindBy of my IRepository to return from this list of mocks based on the a linq query provided in my service that is being tested. </p>
  
  <p>What I have currently have throws an exception and is as follows. What is wrong please?</p>
  
  <pre><code>mock.Setup(moq =&gt; moq.FindBy(It.IsAny&lt;Func&lt;IThing, bool&gt;&gt;()))
              .Returns((enumThingType tp) =&gt; _mockedThingsList.Where(x =&gt; x.ThingType == tp));
  </code></pre>
  
  <p>The repository interface looks like:</p>
  
  <pre><code>interface IRepository&lt;T&gt;
  {
      IEnumerable&lt;T&gt; FindAll();
  
      IEnumerable&lt;T&gt; FindBy(Func&lt;T, bool&gt; predicate);
      void Add(T item);
      void Remove(T item);
  
      bool Contains(T item);
      int Count { get; }
  }
  </code></pre>
  
  <p>And the service that would be tested with this mock</p>
  
  <pre><code>class ThingService
  {
      private readonly IRepository&lt;IThing&gt; _repository;
  
      public ThingService(IRepository&lt;IThing&gt; repository)
      {
          _repository = repository;
      }
  
  
      public List&lt;IThing&gt; GetThings1()
      {
          return _repository.FindBy(y =&gt; y.ThingType == enumThingType.WhatEver).ToList();
      }
  
      public List&lt;IThing&gt; GetThings2()
      {
          return _repository.FindBy(y =&gt; y.Name == "What ever").ToList();
      }
  }
  </code></pre>