Here is my code:
[TestMethod]
public void LoginUnregisteredUserShouldFail()
{
Mock<IRepository<User>> _repo = new Mock<IRepository<User>>();
UserServiceForTest target = new UserServiceForTest(_repo.Object, new HashingService());
var unregisteredTestUser = new User() { Email = "a", Nombre = "test", Password = "test" };
var registeredHashedTestUser = new User() { Email = "test@test.com", Nombre = "test", Password = "qUqP5cyxm6YcTAhz05Hph5gvu9M=" };
Expression<Func<User, bool>> expression = a => a.Email == "a";
_repo.Setup(a => a.Single(It.Is<Expression<Func<User,bool>>>(l => l.ToString() == expression.ToString()))).Returns(unregisteredTestUser);
Assert.IsFalse(target.ValidateCredentials(unregisteredTestUser));
}
I want to query the Single method of my repo, matching Email, and I want the result to be the specified User.
I dont know what I'm doing wrong but I always receive null.
EDIT: My implementation is the following:
private string GetUserPasswordFromDbByUserName(string userName)
{
Expression<Func<User, bool>> ax = a => a.Email == userName;
var axx = ax.ToString();
var user = _repo.Single(ax);
if (user != null)
return user.Password;
else
return string.Empty;
}
It receives a string userName and for some reason, the .ToString() returns 'a => (a.Email == value(Casita.Services.UserService+<>c__DisplayClass5).userName)' instead of 'a => (a.Email == "a")'. Makes no sense to me, but I'm guessing this is the reason the comparison is failing.