Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Test IQueryable

I am trying to write a unit test for a method which takes an IQueryable collection.

How should I instantiate the collection in my unit test before I pass it to the method for test? This is the default code in my test

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

And here is what I tried writing

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

Am I making a school boy error?

like image 584
Adrian S Avatar asked Jul 06 '11 16:07

Adrian S


2 Answers

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

like image 94
Marc Gravell Avatar answered Sep 23 '22 10:09

Marc Gravell


IQueryable seems to be an interface ( http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx ), shouldn't you try to instanciate a class that implements this interface instead (or maybe use a mock object for your test) ?

like image 27
phtrivier Avatar answered Sep 23 '22 10:09

phtrivier