title: Moq Mock lambda expression
body: <p>I am trying to mock a repository on which a lambda expression is executed. The method that I want to execute looks like this:</p>
  
  <pre><code>public SelectListItem GetProtocolById(int protocolId)
  {
      var protocol = UnitOfWork.ProtocolRepository.FindAll(s =&gt; s.PROTOCOL_ID == protocolId).FirstOrDefault();
      return new SelectListItem()
      {
          Text = protocol.USER_DEFINED_ID,
          Value = protocolId.ToString()
      };
  }
  </code></pre>
  
  <p>I created a mockRepository in another class </p>
  
  <pre><code>protected MockRepository mockRepository = new MockRepository(MockBehavior.Default);
  </code></pre>
  
  <p>I have an UnitOfWork and a protocols repositories and I mock them like this: </p>
  
  <pre><code>var protocolRepositoryMock = mockRepository.Create&lt;IRepository&lt;PROTOCOL&gt;&gt;();
  var unitOfWorkRBMMock = mockRepository.Create&lt;IUnitOfWorkRBM&gt;();
  </code></pre>
  
  <p>The FindAll method from IRepository looks like this :</p>
  
  <pre><code>IEnumerable&lt;TEntity&gt; FindAll(Expression&lt;Func&lt;TEntity, bool&gt;&gt; where, params Expression&lt;Func&lt;TEntity, object&gt;&gt;[] includes);
  </code></pre>
  
  <p>And the definition of the IRepository in my IUnitOfWorkRBM is this one:</p>
  
  <pre><code>IRepository&lt;PROTOCOL&gt; ProtocolRepository { get; set; }
  </code></pre>
  
  <p>I have tried to mock the unitOfWorkMock for it to be able to execute the lambda expression like this: </p>
  
  <pre><code> unitOfWorkRBMMock.Setup(s =&gt; s.ProtocolRepository.FindAll(It.IsAny&lt;Expression&lt;Func&lt;PROTOCOL, bool&gt;&gt;&gt;()))
                  .Returns(new Func&lt;Expression&lt;Func&lt;PROTOCOL, bool&gt;&gt;, IEnumerable&lt;PROTOCOL&gt;&gt;(
                  expr =&gt; protocolsList.Where(expr.Compile())));
  </code></pre>
  
  <p>But when I try to call the <code>GetProtocolById</code> method, the protocol comes null.</p>
  
  <p>Any ideas on what I am doing wrong here ?</p>