5

For unit testing purposes, is it possible to do this:

  • Have test data in some format (XML?) checked into source control
  • When unit tests are run, instantiate some kind of in memory database from this test data
  • Have EF run against this in memory database for the duration of the unit test run

?

We don't want our unit tests to rely on a specific external database being present, and in a specific state.

We also don't want to maintain two different "worlds" in our code and tests, the real world where EF runs against a real db and a fake work where our tests run against some kind of EF mock.

2
  • I see contradiction. You don't want to use real database and you don't want to use fake. So what do you want to use? Commented Aug 15, 2012 at 12:42
  • he wants to use a blank in memory database for the tests, that is real from EF:s perspective, but not persistent between test runs and tests are not dependant on an external database server. Commented Sep 13, 2012 at 11:37

1 Answer 1

6

Unit tests should not be dependent on any database. Any dependency on database (even in memory database) means you are doing integration tests and integration tests should be done against the real database you are going to use.

I'm not aware of any XML database for EF but even if it exists you are back in front of your requirement: We also don't want to maintain two different "worlds" in our code and test. Every database has its own EF provider created by different company. Even providers for MS SQL Server and MS SQL Server Compact Edition are different enough to make switching between them quite challenging.

What you should do:

  • Hide all EF usage behind some abstraction (that includes everything including Linq-to-entities queries) and mock this abstraction instead of EF for unit tests
  • Use integration tests against the real database implementation you want to use in production for testing the abstraction itself
Sign up to request clarification or add additional context in comments.

1 Comment

So are you suggesting creating another repository? I see mostly problems with repositories trying to hide EF or other ORM's. DbContext (implementing IDbContext) is already a repository.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.