Testing
Tests are executable documentation of the behaviour a system promises. They exist to let you change code without fear, not to prove you wrote it. That distinction decides almost every question in this section: what to test, how much to mock, and whether coverage is a goal.
The 60-second answer
Section titled “The 60-second answer”“Tests are a safety net for change. I test behaviour at the boundary rather than implementation details, because implementation tests break on every refactor and tell you nothing about the product. Most of the value sits in fast unit tests for pure logic and a thinner layer of integration tests for the places where components meet — database, HTTP, jobs. I keep a few end-to-end tests for the critical paths, because they are slow and flaky but they catch the wiring failures nothing else does. I write a test first when the behaviour is clear and the design is not; I write it after when I am exploring. Coverage is a diagnostic, not a target.”
Why tests matter
Section titled “Why tests matter”| Reason | What it means in practice |
|---|---|
| Confidence to refactor | The suite catches regressions the type checker cannot see |
| Design pressure | Hard-to-test code is usually badly coupled |
| Documentation | A test states the expected behaviour in executable form |
| Faster feedback | Seconds in CI instead of minutes reproducing by hand |
| Debugging aid | A failing test localises the break |
The costs are real too: tests take time to write, they rot, and a slow or flaky suite gets ignored. A suite nobody trusts is worse than no suite, because it trains the team to ignore red.
The shapes of a suite
Section titled “The shapes of a suite”The three names you must distinguish:
- Unit test — one function, class or module, with collaborators replaced by doubles. Fast, precise, brittle to refactors if it asserts internals.
- Integration test — several real components together, often including the database. Slower, but it catches contract mismatches.
- End-to-end test — the whole system through its real interface, usually a browser or HTTP client. Slowest and flakiest, highest confidence that a user flow works.
The Test Pyramid covers the trade-offs and why many teams now argue for a “trophy” shape instead.
What to test
Section titled “What to test”Test the behaviour someone would describe in a ticket:
- The happy path. The main flow returns the right result.
- Boundaries. Empty collections, zero, first and last elements, maximum sizes.
- Failure modes. Invalid input, upstream errors, timeouts, permission denied.
- State transitions. What changes, and what must not change as a side effect.
- Regression cases. Every bug fix gets a test that fails before the fix.
Do not test:
- Framework behaviour (“Rails saves the record”, “React calls the render function”).
- Private methods, unless they are independently complex.
- Exact log lines, CSS class lists, or HTML structure that a designer may change.
- Getters and setters with no logic.
A good test has one reason to fail
Section titled “A good test has one reason to fail”RSpec.describe Order do describe "#total" do it "sums line items and applies the discount" do order = build(:order, discount: 0.1) order.line_items << build(:line_item, price: 100, quantity: 2)
expect(order.total).to eq(180) end endendThe name states the behaviour, the setup is minimal, one assertion group checks one idea, and the expectation is on a value, not on a call sequence. When it fails, the cause is obvious.
Reading order
Section titled “Reading order”- Test Pyramid — unit, integration, e2e, and the cost model behind the shape of a suite.
- TDD — red, green, refactor; outside-in and inside-out; when it helps.
- RSpec — the Ruby tooling: matchers,
let, factories, request and system specs. - Mocking — doubles, seams, and avoiding brittle tests.
- Performance Testing — load, stress, soak, and how to read p95 and p99.
How to talk about testing in interviews
Section titled “How to talk about testing in interviews”Four questions come up repeatedly, and each has a shape a senior answer follows.
- “How do you decide what to test?” — Risk and change frequency. Test the core domain logic and the boundaries where failures are expensive.
- “How do you test this specific feature?” — Name the layer, the seam, the doubles, and the assertions. Be concrete.
- “The suite is slow and flaky. What do you do?” — Measure, find the slowest and flakiest, push logic down to unit tests, replace sleep with deterministic sync, and quarantine what you cannot fix.
- “What is your coverage target?” — Coverage shows what is not tested; it cannot show quality. Use it to find untested branches in critical code.
A candidate who says “we have 100% coverage” without being able to name a test that would fail if a business rule broke has answered the wrong question.
Common traps
Section titled “Common traps”- Testing implementation instead of behaviour. The test passes after a refactor only if you rewrite it, which defeats its purpose.
- One giant test per feature. When it fails you learn nothing about where.
- Asserting on mocks. A test that checks a mock was called proves you wrote the mock.
- Sleeping to wait. Use deterministic waiting (Capybara’s
have_content, fake timers, polling assertions) or you own the flakiness. - Chasing coverage. Untested lines matter, but a covered line with a vacuous assertion is worse.
- Not running tests in CI. A suite that is not enforced is documentation nobody reads.