mercredi 4 mars 2015

Mocking repository and callback issue

I have a Repository which I have mocked using Moq.


I have a Service which I have mocked. The service expects a repository in its constructor and I have successfully been able to do this.


The problem I am now facing is that, I want the Repository to not connect to the DB but instead use an in memory storage to query/perform operations against.


if I do this:



userMockRepository.Setup(r => r.Login(It.IsAny<string>(), It.IsAny<string>()))
.Returns((User u) => users.Find(p => p.Password == u.Password &&
p.Username == u.Username));


then run the test, I get an exception:



Parameter count mismatch



when the Service method is being called when about to invoke the Login on the UserRepository.


What am I doing wrong? Am I misunderstanding?


Essentially any Repository calls should be redirected or query against an in memory collection (i.e List)


this is my actual service layer method:



var user = uoWContext.UserRepository.Login(request.Username, request.Password);

if (user == null)
{
response.Success = false;
response.FailureInformation = "Invalid username or password";
}

return response;


When it gets to the line uowContext.UserRepository.Login(...) - this is where I get the exception.


Aucun commentaire:

Enregistrer un commentaire