Is it possible to mock an EF model so that I can test code which uses the model classes without getting rid of LINQ to Entities code strewn throughout my project? Or will it be necessary to set up a test database for the model to hit instead?
-
See my answer for the question Mock Entity Framework database. A mock object context can be created from your entity model using a T4 template. This is the way the regular POCO classes are created, and it can be extended pretty easily to create a mock object context and interface to make it swappable with your concrete object context.NightOwl888– NightOwl8882013-01-18 19:57:48 +00:00Commented Jan 18, 2013 at 19:57
Add a comment
|
1 Answer
You could wrap your LINQ code in Data Access Objects. The DAOs would have separate interface and implementation code. Your test code would mock the DAOs.
If you don't want to do that, you'll need a test DB.
3 Comments
Chris Charabaruk
Since I'm not a hardcore TDD type, this sounds overly complicated just to test some systems I'm currently working on. Shame EF wasn't designed to be easily testable!
Dave Swersky
EF v2 will not suffer from these complications. We'll be able to do unit testing against POCO objects and lose less hair.
Elad Benda
Even when creating another layer, how would you perform save operation - as EF uses mMamDbEntities.SaveChanges(); after Entity reference has been chnaged