Lesson 16 · Senior testing practice
Choosing mocks vs DB fakes vs real collaborators
How to choose the right stand-in for the behavior you are trying to prove, without overpaying for realism or underpaying for signal.
Your win: explain when a simple mock is enough, when testutil.MockDB is the better fit, and when a heavier collaborator is justified outside the unit-test boundary.
The choice behind every test
Earlier lessons taught you the mechanics of testify mocks and the house DB toolkit. The senior step is choosing between them deliberately rather than by habit.
That sounds small, but it is one of the clearest places where testing maturity shows up. Two people can both write a passing test, but the stronger engineer will usually choose the double that proves the behavior more honestly with less maintenance cost.
What each option is really buying you
A plain mock is great when the point of the test is orchestration: who got called, how many times, with what sort of behavior contract. But if the thing you actually care about is repository scan behavior, SQL-shape logic, or mapping detail, a plain mock may be too weak. That is where testutil.MockDB becomes useful: still cheap, still unit-test territory, but with stronger proof.
And sometimes even that is not enough. That is the moment where a heavier collaborator becomes justified — not because realism is automatically better, but because the lower layer cannot honestly prove the fact you care about.
A practical reading of the options
- Plain mock: fast, simple, best for orchestration and behavior expectations
testutil.MockDB: stronger signal for repository behavior, scan wiring, and SQL shape without a real DB- Real collaborator: only when lower layers cannot honestly prove the thing you care about
The important part is that each option answers a different question. Once you see that, “which one should I use?” becomes much easier to reason about.
Compare the layers
Revisit the repo testing map’s “testing by layer” guidance and compare it to the repository and usecase lessons.
→ Repo testing map
→ docs/testing/lessons/0008-mocking-the-database.html
Check yourself (from memory)
Q1. How should you choose between a plain mock and testutil.MockDB?
Sources. Repo testing map; mocking/database lessons.