Backend & systems·topic 13 of 13
Testing and test doubles
The four words for fake collaborators get swapped constantly, including by people who wrote the tests. Naming them correctly is the fastest way to make a test review argument short.
Read in order · tick what you already know
- 01
you swap the real collaborator for a stand-in, because the real one is slow, remote, or has side effects you cannot have in a test.
Test double
- 02
you hard-code the collaborator to return a fixed value so the code under test has something to work with.
Stub
- 03
you assert that the email service was called exactly once with this address, and the test fails if it was not.
Mock
- 04
you wrap the real thing, let it do its actual job, and record the calls so you can check them afterwards.
Spy
- 05
you write a working in-memory version of the repository, with real behaviour, just not one backed by a database.
Fake
- 06
you save the current output to a file and the test just checks nothing changed, which is exactly what you want on a legacy refactor.
Golden test
- 07
the consumer's expectations are recorded and replayed against the provider's build, so nobody breaks the other by surprise.
Contract test
- 08
the test fails one run in twenty, everyone reruns CI until it goes green, and the suite stops meaning anything.
Flaky test