Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework Core Unit Testing - SQLite in-memory mode vs. The InMemory provider

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?

like image 459
YuvShap Avatar asked Jul 18 '17 09:07

YuvShap


People also ask

Should I use in-memory database for testing?

While some users use the in-memory database for testing, this is generally discouraged; the SQLite provider in in-memory mode is a more appropriate test replacement for relational databases.

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

How does EF core help unit testing?

Testing EF Core Applications Testing is an important concern to almost all application types - it allows you to be sure your application works correctly, and makes it instantly known if its behavior regresses in the future.


1 Answers

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:

  • https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite#writing-tests
  • https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory#writing-tests

...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

like image 182
Helder Sepulveda Avatar answered Sep 18 '22 17:09

Helder Sepulveda