Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
Test doubles: stubs, fakes, mocks and spies
card 4 of 6
Judge by use, not by library name
Library names don't follow the four kinds, so judge a double by how the test uses it.
In one agent-written Java test, `when(repo.find(7)).thenReturn(order)` uses a Mockito `mock` as a stub. In the same test, `verify(mailer).send(any())` uses one as a mock. It checks that a send happened, but never what was sent.
Mockito's `spy()` and Jest's `jest.spyOn` call the real method by default. So a "spy" on a real gateway can still send real charges.
A fake is only as faithful as its author: an in-memory map has no unique constraints.
Terms on this card
- stub
- A test double that returns canned answers and checks nothing about how it was called.
- fake
- A test double that is a working but simplified implementation, such as an in-memory store, which keeps real state between calls.
- mock
- A test double set up with expected calls that makes the test fail if those calls don't happen.
- spy
- A test double that records the calls made to it so the test can assert on them afterwards.
- Mockito
- Popular Java test library. Its `mock()` creates doubles that return defaults until scripted, and `verify` checks calls on them.
Lesson goal: Distinguish the four kinds of test double: stubs return canned answers, fakes are working lightweight implementations, mocks verify interactions and spies record calls. Say what each kind lets a test verify, and what it lets a test silently skip. ·
the whole lesson