Unit Testing and what to do with the database? #31
-
I'm digging into the world of unit testing and TDD. I'm reading the book "Unit Testing - Principles, Practices and Patterns" by Vladimir Khorikov. I came across the statement that you shouldn't stub/mock a database. For a project, we use an in-memory SQLite db. The db is getting cleaned up for each unit test. I was looking into the GitHub project (banking-kat for .NET) and saw the use of a fake repository. I also read a previous discussion about fake repositories. There was also mentioned that it is a bad practice to use SQLite db. 2 things I like to know:
I'm a practical person. So if you have an example or situation description, that would be great! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Great question @Daissy538! Databases are definitely a controversial topic. BACKGROUND Currently, there are three approaches:
Currently, there is no general consensus regarding this, what is better? Why I'm illustrating the usage of test doubles in the banking-data:
Now when I look at code examples in "Unit Testing - Principles, Practices and Patterns" by Vladimir Khorikov. In Section 10.2.1 Managing database transactions in production code, in listing 10.3 he shows that UserRepository is a concrete class (there's no interface!) - and it accepts Transaction (though later he shows a DBContext - CrmContext being passed in). He also mentions in 10.3.3. Avoid in-memory databases and he describes the reason why he doesn't recommend in-memory databases like SQLite. So you will see the following differences between the code samples which I show versus the book:
I currently cannot say that either approach is good or bad. The approach I illustrated is the approach I'm using in practice with teams, it allowed me to maximize the area of unit testability (I can test application logic and transitively domain logic). YOUR QUESTIONS To answer your questions:
Ok I find that approach somewhere in-between unit testing versus integration testing.
Correct, I showcase usage with a test double (here fake repository) as per Classicist TDD style.
Could you send the link?
I don't see SQLite in-memory DB per se, but I do see the following issues:
There's two alternatives:
My recommendation is - you can play around with both of these options on your GitHub, maybe make some examples, and read the material above, to understand both views.... after all that, present to your colleagues. The other point is - is the team experiencing any points with SQLite in-memory? If no pain points are felt, then there generally won't be interest in change. |
Beta Was this translation helpful? Give feedback.
Great question @Daissy538! Databases are definitely a controversial topic.
BACKGROUND
Currently, there are three approaches:
Currently, there is no general consensus regarding this, what is better?
Why I'm illustrating the usage of test doubles in th…