15

I am in a process of writing unit tests for a project that uses EntityFramework Core and according to the docs I can use SQLite in-memory mode or The InMemory provider to approximate the database context.

The docs states that the SQLite in-memory mode behaves like a relational database and that The InMemory provider does not always behave like a relational database.

As far as I understand the SQLite mode sounds better because it behaves like relational database while the InMemory provider does not, but I guess there is other aspects to consider otherwise noone will use The InMemory provider which sounds a lot worse.

Is there other pros and cons to each approach I should consider before I choose which tool to use?

2
  • 6
    I disagree with the opinion-based votes. Objectively enumerating the pros and cons is valuable to the community. Commented Jul 19, 2017 at 16:58
  • 2
    Unfortunately I think this just not mature enough for some one to have used it enough to tell you the horror stories that might happen. Commented Jul 25, 2017 at 23:21

3 Answers 3

8
+25

If your sole purpose is writing Unit Tests, look closely at the boiler plate code needed to create the tests, that could impact your deadlines... I would go with the option that makes me type less code! (The InMemory provider looks simpler).

Look at the samples and decide:

...and of course your project will have Integration Tests, on those you will connect to the real database and do additional checks. That's why for the unit tests my main concern is writing time, not so much how the mock DB behaves

Sign up to request clarification or add additional context in comments.

3 Comments

I totally disagree with the boiler plate code being the concern here - it could be refactored properly to a shared method - should not significantly increase your footprint. Yet I agree with your comment about the distinction between unit tests and integration tests. I think you should add this comment to the answer.
Writing less code is awesome, but the main concern for a unit test is to get it working as close to the actual code as a unit test can get. One line of code or a million, if your test doesn't exercise the business logic it is useless.
@TerryH if to get it working close will take a million lines of code or an extra 80h of time most project manager will tell you to take a different route, at the end the closest to reality are integration tests not the unit tests ... Unit Testt are not meant to catch every error in the code, they are quick checks to find most problems early on
1

In my experience (I'm the author of Mig#), each database platform has its own little quirks and often they behave slightly different (e.g. look at the comments in the Supports attributes of SQLiteProvider.cs). Even if everything is seemingly abstracted away from you using EF, there might be subtle differences giving you false positives or false negatives in your tests.

Therefore, my suggestion to you is: do integration testing with the actual database platform you want to support (even creating, dropping a SQL Server database doesn't take so much time, and there are other strategies how to optimize integration testing involving databases). For your business logic, use smoke testing with no database in the background at all.

This has the additional benefit that you will structure your class design to have database access separate from business logic.

Comments

0

AS per the documentation If you are using InMemory Provider against SQL lite there is little behavior difference in loading relational data.

For e.g. If there are related child records. Using LINQ all the child records will load automatically which is not in case with actual database where you need to explicit call include function.

The other scenario will be I/O perfomacne difference while connecting to database in remote location.

It's advisable to include some real testing not to be amazed later on by real application performance and behavior

Comments

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.