Chapter 9

Dishonest Tests

If you need mocks, your architecture is the bug.
Demo: Test that an order total is calculated correctly
☠ Class-based test: mock ceremony
Test result
waiting...
✦ Pure function test: assert
Test result
waiting...
0
Lines of test code
0
Lines of test code
0
Mocks, stubs and container set up
0
Mocks, stubs and container set up

What you just saw

The left panel spends thirty lines creating the conditions for the test to run: mocking the coupon registry, mocking the tax service, wiring the dependency injection container, constructing the Order object with its mocked dependencies, calling the method, then verifying the mocks were called with the expected arguments. The actual assertion, the thing the test exists to verify, is one line buried at the bottom. Most of the test is about the architecture, not the behavior.

The right panel is the entire test. Call the function with inputs. Assert the output equals the expected value. Eleven lines in all: three of data, one call and the assertion. The function has no dependencies to mock because it takes everything it needs as arguments and returns everything it produces as a return value. The test tests the behavior, not the wiring.

How this works: This demo visualizes test ceremony, not timing. The class-based test takes 48 lines and sets up nine things: three mock objects, five stubbed methods and a dependency-injection container. The pure function test takes 11 lines and sets up none. Counts are from real code. Source: github.com/adamzwasserman/honest-code-traces
← Ch.8: The Performance Lie Ch.10: Dishonest AI →