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.
Mocks that test nothing
card 3 of 6
Three tells and one question
builds on: Test doubles: stubs, fakes, mocks and spies, What makes a test able to fail
1. **The unit under test is mocked or patched itself**: `patch.object(Checkout, ...)`, Mockito `spy` plus `doReturn` on the service, `jest.spyOn(svc, ...)` plus `mockReturnValue`.
2. **An assertion checks a mock's canned value**: the mock returned what the test configured it to return.
3. **Only call checks** (`assert_called*`, `verify`, `toHaveBeenCalled`) and no check of a result or state.
**Later cost** of all three: the suite stays green while the logic is wrong, so bugs ship and refactors look safe.
**Settle it:** if the unit's logic were wrong, would this test go red?
Terms on this card
- unit under test
- The function, method or class a test is meant to check. Its real code must run in the test.
- mock
- A test double that is preconfigured with answers and checks which calls it received.
- spy
- A test double that wraps or records calls so a test can inspect them. In Mockito, it wraps a real object.
- patch.object
- Python unittest.mock helper that temporarily replaces an attribute or method of an object or class with a mock.
- Mockito
- The common Java library for creating mocks and spies and verifying calls on them.
Lesson goal: Recognise tests that mock the unit under test, assert that a mock returned what it was configured to return, or check only that calls happened without checking any outcome. Rewrite such a test to exercise real logic, using fakes where possible. ·
the whole lesson