I’m watching this video post (by Jon Galloway and Jesse Liberty) about Building a repository for testing, and they mentioned it’s not a good idea to have the DB, and rather one should use a fake repository. The two reasons they gave for that are:
1. The DB may not be available, and
2. Unit tests should be focused at the code level.
So about the first, I never encountered a scenario where I wanted to work on anything and test env. DB wasn’t available, and the second point they made I didn’t get.
So is it a bad practice using your DB in unit tests? What is the potential harm?
Thanks.
Unit tests should test one thing and have no external dependencies (i.e. test in isolation).
As soon as the file system or a database, or anything external is involved, it is really an integration test.
If a unit test relies on an external dependency, it requires a known state and therefore becomes prone to breaking (fragile).
As @Darin mentions, you can include a Database in an integration test by ensuring a known state by restoring a known database state, running a test in a transaction and rolling back the transaction at the end of the test.