Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

FreeAgent Engineering 6 min read Opinion mediumAdept

Spec-tacular: ways to think about writing expressive RSpec

Structuring RSpec like a scientific experiment, with shared setup as control variables, `subject` as the dependent variable and `let` inside contexts as the independent variable, makes specs read as documentation of a class's interface. Implicit descriptions via `subject` and `is_expected` work well for plain Ruby objects. View and controller specs usually benefit from hand-written descriptions that state behaviour, for example "includes the delete button" instead of a CSS selector. For controllers, a named `subject(:response)` returning `@response` combined with compound matchers lets several expectations share a single request. Nesting deeper than two contexts adds complexity, and Minitest's plain-Ruby four-phase style is a fair alternative, but RSpec's expressiveness is worth it if you treat tests as documentation.

Related posts

  • What if writing tests was a joyful experience?

    Jane Street Tech Blog mediumAdept

    Expect tests (snapshot tests) let you write a blank `[%expect]` block under code that prints output; the test runner fills in the actual output as a diff you can accept in your editor, making test-writing feel like a REPL session. Jane Street uses them for most of its OCaml test suite, from rounding-function tables and Bonsai web UI DOM diffs to magic-trace symbol demangling and order-book state machines. Because they capture whole printed state rather than hand-picked assertions, they implicitly guard against regressions you didn't anticipate. Good expect tests depend on helper functions and pretty-printers that keep the output concise and readable.

  • Focus On Behavior, Not State, for a More Maintainable Codebase

    Shopify Engineering mediumAdept

    When adding pre-orders and try-before-you-buy to Shopify's Purchase Options APIs, code that equated "has a selling plan" with "is a subscription" forced repeated refactors. The fix was to model behavior rather than state: wrap selling plans in an object that answers questions like "has recurring deliveries?" or "is billing deferred?" (upfront/deferred/recurring × billing/delivery), so payments, delivery and inventory code ask only what they need and conditionals disappear. To shield the data team from schema churn, a separate reporting table records these traits and labels per order, acting as a translation layer between platform code and merchant-facing domain concepts.

  • Reliving Your Happiest HTTP Interactions with Ruby’s VCR Gem

    Shopify Engineering mediumAdept

    VCR records live HTTP interactions into YAML "cassettes" and replays them in Ruby tests, giving accurate fixtures for complex, slow, or poorly understood APIs where hand-built Webmock stubs drift from real behavior. Use VCR for multi-call integration tests and unpredictable APIs, and keep Webmock or decomposed unit tests for simple, well-understood calls or state that is hard to reproduce. Practical advice includes avoiding the default :new_episodes record mode in favor of :once or :none, logging requests with before filters, scrubbing secrets with filter_sensitive_data, isolating erratic auth calls into their own cassettes, and deleting cassettes liberally.

  • Enhancing the Mock Server: A User Interface Approach

    Zalando Engineering mediumAdept

    Zalando's team built a UI on top of mirage.js so product managers and designers can switch which mocked response each API endpoint returns on a staging build, without code changes or redeploys. A React Context provider re-instantiates the mock server on demand, and the selected scenario is kept in session storage under an app-unique key so it survives the page reload that loading a new data set requires. The approach covers edge cases like stale or missing data, but it complements rather than replaces unit and contract tests, and the frontend-defined mocks must be kept in sync with the backend schema. A generalized version now ships as an internal NPM module.

  • Recycle your tests: many shades of green

    trivago tech mediumAdept

    trivago reuses a small subset of its existing Selenium/Cucumber end-to-end scenarios (via its internal framework trupi) for three extra jobs by adding Gherkin tags rather than new test code. The jobs are synthetic monitoring of production every 10 minutes across three GCP regions with Slack alerts, collecting lab-style RUM metrics (CSS/JS/image sizes, GraphQL call counts) after each release, and checking stage stability after backend changes. Running so many tests against stage degraded it, so the lesson is to reuse selectively. Recyclable tests share a few traits: configurable targets, no side effects, safe repetition, meaningful assertions, visible failures, and a small, fast footprint.

The quest for software engineering knowledge.

Look:
Layout: