I am watching this video publish (by Jon Galloway and Jesse Liberty) about Creating a repository for testing, plus they pointed out it's not recommended to achieve the DB, and rather you ought to make use of a fake repository. The 2 reasons they gave for your are:
1. The DB might not be available, and
2. Unit tests ought to be focused in the code level.
So concerning the first, I never experienced a predicament where I needed to operate on anything and test env. DB wasn't available, and also the second point they provided I did not get.
Same with it a poor practice making use of your DB in unit tests? What's the potential harm?
Same with it a poor practice making use of your DB in unit tests?
It's bad to make use of database in unit tests although not in integration tests. The thought of unit tests would be to test the various layers of the application in isolation. Tying these to a database means they are no more unit tests because they rely on a database, so that they contradict probably the most fundamental rules of the unit test.
Then when time involves test out your actual data access layer (the main one that's striking the database) you need to use integration tests. In individuals tests you need to setup an evaluation database. Ideally this database ought to be inside a known condition for every integration test. To do this you might have scripts that induce the database within the test fixture setup and execute the whole integration test right into a single atomic transaction that'll be folded in the exam fixture destroy to ensure that the database is again inside a known condition for the following integration test.
Another approach (if you work with an ORM) would be to setup your computer data access layer within the integration test to make use of an in-memory database (for example SQLite) which is produced and thorn lower for every test.
Unit tests should test one factor and also have no exterior dependencies (i.e. test in isolation).
The moment the file system or perhaps a database, or anything exterior is involved, it becomes an integration test.
If your unit test depends on an exterior dependency, it takes a recognized condition and for that reason becomes vulnerable to breaking (fragile).
As @Darin mentions, you can a Database within an integration test by making certain a recognized condition by rebuilding a recognized database condition, managing a test inside a transaction and moving back the transaction in the finish from the test.