How can we improve Entity Framework Core?

Make Entity Framework fully mockable and injectable

Make Entity Framework Fully Mockable and injectable

In general, mocking out the Entity Framework DAL is a big pain to accomplish. ObjectContext has no interface and the T4 templates aren't written to create interfaces for the objects they create.

This hampers good unit testing and makes them less injectable.

Put an interface on ObjectContext. Have the T4 Templates put interfaces around all of the objects that are generated and make sure that all objects used have interfaces so that mocking frameworks, like MOQ or Moles & Pex can automatically generate stubs to replace the objects in unit tests so that tests don't need to be tightly coupled to the database.

193 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Francis OuelletFrancis Ouellet shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    Marking as completed as we have made advances in EF Core towards this (and we did previously in EF6) and we don’t plan to go any further.

    In general we recommend against trying to mock EF API behaviors beyond simple cases because they are too complex and dependent on the underlying providers and databases. Instead we recommend either using in-memory databases or creating abstractions such as repositories in applications, which can be more easily mocked.

    3 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base