如何测试我的linq存储库调用 [英] How to test my linq repository calls
问题描述
我有以下UserRepository:
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();
}
}
"FindAll"方法是存储库的一部分,基本上只是调用context.GetTable().AsQueryable()
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.
我做了很多搜索,似乎碰到了所有关于模拟存储库并返回列表(通常是在单元测试开始时创建的列表)的话题,但是我不知道这是什么用途.我想确保我编写的linq查询实际上是正确过滤对象并仅检索所需的记录. 由于DataContext没有接口,所以我不能简单地模拟它.
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?
推荐答案
感谢@Daniel Higarths的最后评论,他在上面提到了Moles,这与下面的视频
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
这篇关于如何测试我的linq存储库调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!