How do I mock IQueryable<T>

爱⌒轻易说出口 提交于 2019-12-03 01:02:23

I usually do exactly what you ended up doing in your test. When writing my tests I assume that the .Net library classes work correctly and don't contain bugs, so I can use them in the tests. When I need a test list, collection, queryable, dictionary, etc. I just create the real thing and populate with test data. It makes the tests much more readable and quicker to write, and to be honest the risk is non-existent.

If you want to mock out your repository, you won't be mocking IQueryable. Instead, mock out the methods of your repository to return fixed, known values (like your second example) that can be used to run your unit tests.

I know this is an old question, but just want to add my 2 cents.

I had the same issue with the repositories generated with SharpLite, which is a ASP .NET MVC framework I use from time to time. After some time, I found out a solution, the only issue is that is using Moq, and not Rhino Mocks, but possibly you can find a way to adapt it. I made a blog post here on how to do it.

It's basically creating a list that implements IQueryable and using it as a fake data background. Hope I can help!

I’m not sure if this will help you… but I did something like what you are talking about. In my scenario, I had a data context class that used the repository.

I started off by creating an interface (IRepository) that included the IQueryable method. Then I created two classes that implement this interface. One class uses an ORM for data manipulation (DbEntityRepository) and another class uses a class property (MemoryRepository). The data context class had a constructor that required the IRepository. Doing this I could use the MemoryRepository for when Testing the data context and I could use the DbEntityRepository for the Application.

If you are interested… you can find the code on codeplex: IQToolkitContrib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!