Mocking ObjectContext in EF4 for unit testing?

entity-framework mocking objectcontext typemock

Question

Is it possible to accomplish that without TypeMock Isolator? There are a few alternatives I've read online, including using a connection string that simply contains metadata, but none of them, apart from TypeMock, appear to allow for the injection of a mock ObjectContext into services for unit testing. Do I spend the money on TypeMock, or are there other options? Has someone succeeded in developing an open source alternative to TypeMock?

1
7
4/24/2010 3:24:47 AM

Accepted Answer

I can test EF4's units without the need for mimicking. Making use of the code from http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/ as a starting point, I constructed a repository interface.InMemoryRepository<T> a class using theIRepository interface. Then I changed theIObjectSet<T> with aList<T> and adjusted the retrieval techniques inside the lesson.

Therefore, pass in the InMemoryRepository rather than the DataRepository if you need to do unit testing.

4
5/21/2010 5:57:32 PM

Popular Answer

Put your Linq2Entity query behind an interface and do isolation-only unit testing on it using a live database.

Mock up your query interfaces and write tests for your business logic. Keep Linq out of your business logic!

Use of RepositoryPattern is prohibited.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow