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 5 of 6
Stub only vs stub plus a call check
Python's `Mock` becomes whatever kind of double the test uses it as. Here `cart_of(4999)` builds a 4999-cent cart in EUR.
In BAD, `gw` is a pure stub. The test proves `checkout` marks the order paid when the gateway says ok. Nothing checks what was charged, so charging 0, or 49.99 instead of 4999 cents, stays green. Later cost: real customers are charged the wrong amount.
BETTER adds a spy-style assert after the call that pins amount and currency. A separate test should stub a failed charge.
// Python
# BAD
gw = Mock()
gw.charge.return_value = {"ok": True}
order = checkout(cart_of(4999), gw)
assert order.status == "paid"
# BETTER
gw = Mock()
gw.charge.return_value = {"ok": True}
order = checkout(cart_of(4999), gw)
assert order.status == "paid"
gw.charge.assert_called_once_with(
4999, "EUR")
Terms on this card
- stub
- A test double that returns canned answers and checks nothing about how it was called.
- 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.
- later cost
- The concrete damage a shortcut does after merge, such as a hidden bug, corrupted data or a maintenance burden.
- `Mock`
- Class in Python's `unittest.mock`. It accepts any call, records it, and can return scripted values. It is used as a stub, spy or mock.
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