Unit Testing in Entity Framework 4 - using CreateSourceQuery
- by Adam
There are many great tutorials on abstracting your EF4 context so that it can be tested against (without involving a DB). Two great (and similar) examples are here:
http://blogs.msdn.com/b/adonet/archive/2009/12/17/walkthrough-test-driven-development-with-the-entity-framework-4-0.aspx (oops, not enough rep. points to post second URL)
basically you wind up querying your repository using linq-to-objects while testing, and linq-to-entities while running, and usually they behave the same, but when you start hitting more advanced functionality, problems arise.
Here's the question. When using linq-to-objects against IObjectSet (ie, unit testing), CreateSourceQuery returns null, which will probably cause your entire query to crash and burn. ie
O = db.Orders.First();
O.OrderItems.CreateSourceQuery().ToList();
Is there a way to get CreateSourceQuery to just return the underlying collection, rather than null when working with collections? Unfortunately EntityCollection is sealed, and so cannot be mocked.
This isn't really the end or the world if EF4 won't let you abstract things to this level, I just wanted to make sure there wasn't something I was missing.