I have a Generic repository and are trying to cast the .Returns to a Expression but it refuse… My code is following:
public RepositoryTest()
{
IList<MockObjectSet> mocks = new List<MockObjectSet>()
{
new MockObjectSet { FirstName = "Beta", LastName = "Alpha", Mobile = 12345678 },
new MockObjectSet { FirstName = "Alpha", LastName = "Beta", Mobile = 87654321 }
};
var mockRepository = new Mock<IRepository<MockObjectSet>>();
mockRepository.Setup(x => x.GetBy(It.IsAny<Expression<Func<MockObjectSet, bool>>>()))
.Returns((Expression<Func<MockObjectSet, bool>> predicate) => mocks.Where(predicate).ToList());
}
It just say
Delegate System.Func<System.Collections.Generic.IEnumerable<expWEBCRM.Tests.Repositories.MockObjectSet>> does not take 1 arguments
Thanks in advance!
You need to explicitly specify the type parameters of the
Returnsoverload like so:EDIT The repository takes an expression and uses it on a
IQueryable. The mock data source is actually anIEnumerable. The difference in the LINQ interface is one takes a lambda, the one an expression:What happens in this scenario is trying to call
IEnumerable.WherewithExpression<Func<T,bool>>. The easiest way to fix this is to have the source collection asIQueryable: