If I have an application that uses a database as its data store (and ultimate source of truth) that brings some obvious issues when developing tests:
There are numerous ways to handle this with many names like mocking, stubbing, doubling, etc. Many holy wars are fought on what is the "correct" way to handle this.
Ultimately, I want a framework that works equally well in testing and in the application, that requires the least effort to use.
Assumptions/Criteria:
What have patterns have you guys used that makes switching between the real application and the tests the simplest and cleanest? Where in the process do you substitute fake test objects for the real ones? Which method sucks the least in practice?
Example: Have the database and the fake test database share a data access layer interface. In the application use an implementation of the interface that uses the database, and in test code use an implementation that uses test data.
- In the applications, objects should ONLY be hydrated from the database (except new ones about to be inserted into the database)
- In testing, objects should be created from test-specific data and not from the database
- In the applications, object behaviors alter the underlying database
- In testing, object behaviors should NEVER alter the underlying database
- Etcetera, etcetera
There are numerous ways to handle this with many names like mocking, stubbing, doubling, etc. Many holy wars are fought on what is the "correct" way to handle this.
Ultimately, I want a framework that works equally well in testing and in the application, that requires the least effort to use.
Assumptions/Criteria:
- Team doesn't care if one error breaks many tests. (IOW, there is no need to write the tests so that the failed test specifically pinpoints where the error is.)
- Use real objects if possible and a double/stub/mock if it's awkward to use the real thing. The database is the main component that needs to be faked in testing, since it makes no sense to have tests altering real production data in the database.
- Test code should look as similar to real code as possible.
What have patterns have you guys used that makes switching between the real application and the tests the simplest and cleanest? Where in the process do you substitute fake test objects for the real ones? Which method sucks the least in practice?
Example: Have the database and the fake test database share a data access layer interface. In the application use an implementation of the interface that uses the database, and in test code use an implementation that uses test data.