Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use an In-Memory Database instead of mocking out my Repositories?

I like the idea of using an In-Memory Database such as SQLite when testing instead of creating Mocks for my Repositories. This way I can also test the code of my Repositories without any performance issues.

What are the pros and cons of this strategy?

like image 390
jimmystormig Avatar asked Jan 30 '10 14:01

jimmystormig


People also ask

Should I use in-memory database?

The main use case for in-memory databases is when real-time data is needed. With its very low latency, RAM can provide near-instantaneous access to the needed data. Because of the potential data losses, in-memory databases without a persistence mechanism should not be used for mission-critical applications.

Should I use in-memory database for testing?

EF Core In-Memory Database Provider The provider is maintained as part of the Entity Framework Core Project. The In-Memory provider was not designed for use outside of testing environments and should never be used as such.

What are the disadvantages of in-memory database?

Cons of in-memory databases The use of an RAM means faster access on the one hand, but also brings with it a key disadvantage: the data stored is only temporary. If the computer system should crash, all temporarily-stored data would be lost.

Why would you use an in-memory database?

In-memory databases can persist data on disks by storing each operation in a log or by taking snapshots. In-memory databases are ideal for applications that require microsecond response times or have large spikes in traffic such as gaming leaderboards, session stores, and real-time analytics.


1 Answers

If I'm using an ORM, I generally tend to use a sqlite in memory database for testing my apps since I don't have the overhead of setting up the mocks and expected results. There's a speed boost over using a dummy database but I don't expect it to be faster than using a mock. If I were using an SQL adaptor, I would probably use a mock since otherwise, I'd probably have to rewrite the calls.

like image 170
Noufal Ibrahim Avatar answered Sep 23 '22 08:09

Noufal Ibrahim