for unit-testing my asp.net MVC web application, I'd like to mock my IGenericRepository (I'm using Moq).
The method that has to be mocked looks like this:
IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");
I've already mocked the repository without any problems:
useraccountRepository = new Mock<IGenericRepository<Useraccount>>();
Now I'd like to tell Moq that when the Get method of my interface is called, a list of useraccounts should be returned:
useraccountRepository.Setup(r => r.Get(u => u.Email == useraccount.Email && u.Password == useraccount.Password)).Returns(useraccounts);
I think there is a mistake in my second lambda because this one works:
useraccountRepository.Setup(r => r.Get(null, null, "")).Returns(useraccounts);
But where's my error?
What also works:
useraccountRepository.Setup(r => r.Get(u => u.Email == useraccount.Email
&& u.Password == useraccount.Password, null, "")).Returns(useraccounts);
It looks like the default parameter values don't apply in my mock. Why is that?
If I use
useraccountRepository.Setup(r => r.Get(u => u.Email == useraccount.Email
&& u.Password == useraccount.Password, null, "")).Returns(useraccounts);
anyway, then the method in my controller throws an exception when the Get message is called:
private bool validateUser(Useraccount useraccount)
{
try
{
Useraccount useraccountLogin = UnitOfWork.UseraccountRepository.Get(
u => u.Email == useraccount.Email && u.Password == useraccount.Password).Single<Useraccount>();
return (useraccountLogin != null);
}
catch (Exception exc)
{
return false;
}
}
Where are my mistakes?
Thank you in advance. Michael