Saturday, May 16, 2009

Mocking IQueryable

On my current project, we’ve been pretty successful in mocking out all of our LINQ to SQL repository operations, with one exception. One of our new methods returns an IQueryable object. No problem I think, I’ll just set an expectation on that call and return a list of the property entity type. But that gives me a build-error that I can’t convert a List<T> to IQueryable<T>.

A quick google yields the simplest of solutions to mocking IQueryable: the “AsQueryable” extension method. So now, my mock expectation returns something like “List<T>.AsQueryable” and all is well. This finding also clued me into a whole slew of interesting looking extension methods in the System.Linq namespace…..

2 comments:

  1. Exactly what I needed. If this was StackOverflow you'd get a +1 :-D

    ReplyDelete
  2. Exactly what i needed as well, thank you soooo much.
    and yes if this was on StackOverflow, i would also give +1

    ReplyDelete