i am new on mock and i am trying to do this mock example :
Repository.cs
public class Repository : IRepository
{
public List<Person> GetForExpression(Expression<Func<Person,bool>> expression )
{
... //get person from orm using expression
}
}
PersonService.cs
public class PersonService
{
private IRepository _repository;
public PersonService(IRepository repository)
{
_repository = repository;
}
public List<Person> GetAllPersonsWith18More()
{
var result = _repository.GetForExpression(x => x.Age > 18);
return result;
}
}
Test:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var moqRepository = new Mock<IRepository>();
var service = new PersonService(moqRepository.Object);
Expression<Func<Person, bool>> criteria = y => y.Age 18;
moqRepository.Setup(x => x.GetForExpression(It.Is<Expression<Func<Person, bool>>>(y => y == criteria))).Returns(new List<Person>());
service.GetAllPersonsWith18More();
moqRepository.VerifyAll();
}
}
if i use this setup works: moqRepository.Setup(x => x.GetForExpression(It.IsAny>>())).Returns(new List());
but i want to use more specific criteria, this is only one example that i do to demonstrate what i need.
This example is not matching, can anyone help to understand why and solve this problem?