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