问题
I have the following UserRepository:
public class UserRepository : Repository<Entities.User> {
public Entities.IUser Find(string domain, string username, string password) {
return (from u in FindAll()
where u.Account.SubDomain == domain && u.EmailAddress == username && u.PasswordHash == password
&& !u.IsArchived
select u).FirstOrDefault();
}
}
The "FindAll" method is part of Repository and basically just calls context.GetTable().AsQueryable()
I want to be able to write a test that will call this repository method but instead of accessing the database use some in-memory store.
I have done lots of searching and everything I seem to come across talks about mocking the repository out and returning a List (usually created at the start of the unit test) instead but what I don't understand is what use is that. I want to make sure that the linq query I have written is actually filtering the objects correctly and retrieving only the required records. As the DataContext doesn't have an interface I can't simply mock that out.
The only other way I can see handling this is to actually talk to the db but I know this isn't the point of unit testing.
My question is how have others handled this?
回答1:
You don't need an interface to mock things. You could easily create a mock that derives from DataContext
and pass that into your repository.
回答2:
Thanks to @Daniel Higarths last comment above where he mentioned Moles this helped me to solve the issue along with the following video http://dimecasts.net/Content/WatchEpisode/170
来源:https://stackoverflow.com/questions/7076726/how-to-test-my-linq-repository-calls