Skip to content

Lesson 9 of 9 · 29m

Rails Testing Culture and Scaling the Monolith Deliberately

Build a fast confidence suite with unit, integration, system, job, and migration tests while scaling throughput through measurement rather than rewrites.

Rails has a testing culture because framework speed is worthless if every deploy is a leap of faith. But a large test suite can become its own tax: brittle system tests, factories that create half the database, and mocks that prove only that the mock was configured correctly. The goal is not coverage. The goal is cheap confidence at the seams where change hurts.

Test behavior at the cheapest honest layer

Model tests belong around invariants and domain behavior. Job tests belong around idempotency and side effects. Request or integration tests belong around authorization and workflow contracts. System tests are expensive; reserve them for critical browser behavior.

test "stranger cannot finalize invoice" do
  sign_in users(:outside_user)
  post finalize_billing_invoice_path(invoices(:acme_invoice))
  assert_response :forbidden
  assert_not_equal "finalized", invoices(:acme_invoice).reload.status
end

This test proves a trust boundary. It does not need a browser.

Factories are construction tools, not fixtures for reality

Factories are useful when they create minimal valid objects. They become dangerous when create(:user) silently builds an account, subscription, payment method, organization, and audit trail. Slow suites often reflect object-graph coupling more than test count.

user = users(:owner) # stable fixture for simple domain state
account = Account.create!(name: "Northwind") # explicit when construction matters

Use fixtures for stable relational data and factories for edge-specific construction. Do not turn the choice into religion; measure suite speed and clarity.

Test asynchronous work as asynchronous work

perform_enqueued_jobs is useful in tests, but do not let it hide queue semantics. Assert that the right job was enqueued, that retry behavior is sane, and that performing twice does not double-charge, double-send, or duplicate rows.

assert_enqueued_with(job: GenerateInvoicePdfJob, args: [invoice.id, user.id]) { post invoice_pdf_path(invoice) }

Then test the job's idempotency separately. The integration and the worker contract are different failures.

Scaling starts with evidence

Before splitting the monolith, answer narrower questions. Is CPU saturated in Ruby or Postgres? Is latency queue time, query time, view rendering, external HTTP, or memory pressure? Are the hottest paths tenant-specific? Can caching, indexes, batching, or queue isolation solve the problem for another year?

Measure: request latency by action; SQL time and query count
Measure: queue age by queue; memory per Puma worker
Measure: external API latency

Horizontal scaling is often correct. Add web processes, isolate worker pools, move read traffic carefully, and cache expensive representations. Microservices are an organizational scaling tool, not a performance spell.

Extract only when the boundary already exists

A good extraction candidate has a narrow data contract, independent scaling need, clear ownership, and tolerable failure modes. A bad candidate shares tables with everything else and exists because the monolith feels unfashionable. Extracting the latter multiplies distributed-system cost while preserving the original coupling.

Field notes

  • System tests that randomly fail erode trust faster than missing tests; quarantine and fix them deliberately.
  • Parallel testing depends on isolated databases and careful use of shared external resources.
  • Feature flags need tests for both states when they guard meaningful behavior.
  • Scale work should end with a before/after measurement and a rollback story, not just a larger instance count.

Was this lesson helpful?

Sign in to rate