Chapter 10

Testing Honest Code

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
Mock objects created
0
Mock objects created

What you just saw

The left panel spends twenty 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. Three lines. 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 requires 46 lines and 10 mock objects. The pure function test requires 3 lines and 0 mocks. Counts are from real code. Java harness confirms: mock-heavy test setup runs 4.1× slower in nanoseconds. Source: github.com/adamzwasserman/honest-code-traces
← Ch.9: The Performance Lie Ch.11: Working with AI →