Entity Framework Context Mocking

entity-framework mocking unit-testing

Question

I'm using the entity framework to access my database and I want to mock the database context inside my unit tests so that I can test my middle tier classes free of their dependency on real data. I know that I'm not the first to ask about this (Mocking an Entity Framework Model), but after some googling I have an instinct that it might be possible to instantiate the context based on the model's metadata alone.

Has anyone been able to do this?

1
19
5/23/2017 12:14:10 PM

Accepted Answer

You can do it with just metadata, there's a good article on it, and unit testing EF in general, here.

6
4/2/2009 4:15:54 PM

Popular Answer

A well known way of doing this is to use the Repository pattern. This acts as a layer over your concrete data access implementation and provides a place to inject test doubles.



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