Unit Test IQueryable

I am trying to write unit test for a method that accepts an IQueryable collection.

How do I instantiate a collection in my unit test before passing it to the test method? This is the default code in my test

IQueryable<Item> Items = null; // TODO: Initialize to an appropriate value 

And here is what I tried to write

 IQueryable<Item> Items = new IQueryable<Item>; // TODO: Initialize to an appropriate value 

Am I making a school boy mistake?

+6
collections generics unit-testing iqueryable
source share
3 answers

Well, you can use .AsQueryable() for any typed set / list / array, however IMO you cannot unit test as different query providers support different parameters / methods. You can only integrate-test this. Any unit test (especially using objects) does little or nothing to prove to your system.

+15
source share

IQueryable seems to be an interface ( http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx ), should you try to implement a class that implements this interface instead (or perhaps use an object layout for your test)?

+2
source share

The best way is to use a template that allows you to test IQueryable responses from a database such as Unit of Work and Warehouse Template

The best way to create a false repository for use in unit testing is with a memory repository , but you can also use Moq or any other fake repository library.

0
source share

All Articles