agentsclimarketplace

Rails rspec testing

Skill railslove/rails-superpowers/skills/rails-rspec-testing

Use when writing or reviewing RSpec specs, choosing between request/system/unit specs, mocking a dependency, or dealing with a flaky or time-dependent test. Triggers on "RSpec", "flaky test", "spec fails intermittently", "mock", "stub", "Timecop", "travel_to", or any file under spec/. Always invoke before writing a new spec file.From its SKILL.md

Install
npx -y skills add railslove/rails-superpowers --skill rails-rspec-testing

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

2 things to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

SKILL.md

3.7 KB, as published. Nobody here has run it

Rails RSpec Conventions

RSpec is the standard test framework here (not Minitest). These rules exist to keep specs deterministic and meaningful, not just green.

No Time-Based Flakiness

Never rely on real wall-clock time in code under test or in the spec itself. Use ActiveSupport::Testing::TimeHelpers (travel_to, freeze_time) — never sleep, and never assert against a freshly-computed Time.current in the same line the code under test also computes it.

# Wrong — flaky, depends on how fast the test runs
it "expires after 1 hour" do
  token = GenerateLoginCode.call
  expect(token.expires_at).to eq(Time.current + 1.hour)
end

# Right — time frozen for the whole example
it "expires after 1 hour" do
  freeze_time do
    token = GenerateLoginCode.call
    expect(token.expires_at).to eq(Time.current + 1.hour)
  end
end

Mock Only External Services

Stub/mock third-party boundaries (payment gateways, external APIs, mailers' delivery). Never mock your own app's models or service objects — that tests your mocks, not your code, and hides real integration breakage.

# Wrong — mocking your own service hides real bugs
allow(RegisterUser).to receive(:call).and_return(user)

# Right — mock only the external boundary
allow(StripeClient).to receive(:charge).and_return(success_response)

If setting up real collaborators is painful, that's a signal the collaborator needs a better factory or the code needs restructuring — not a signal to mock it away.

Request vs. System vs. Unit Specs

Spec typeUse for
UnitService objects, models, query objects, jobs — the default, fastest
RequestController behavior: status codes, redirects, JSON shape
SystemA full user flow through the browser (JS, multiple pages) — expensive, use sparingly

Default to unit specs for anything with a .call. Reach for a request spec only to verify the controller wiring (params → service → response), not to re-test the service's branches — those belong in the service's own unit spec, per rails-service-objects. Reach for a system spec only when the behavior genuinely spans multiple pages or requires JS (Turbo/Stimulus interaction) — not as a default "test it end-to-end" habit.

FactoryBot

  • No default associations on a factory unless every use needs them — build associations explicitly per example via traits, so a spec's setup states what it actually depends on.
  • Traits over conditional factory logic (trait :admin { role { "admin" } }, not an if inside the factory block).
  • No fixtures — FactoryBot only.

Reviewing Existing Specs

  1. Any sleep, unwrapped Time.current/Date.today comparison, or real HTTP call? → freeze time / use travel_to / stub the external client.
  2. Any allow(...).to receive on an internal model or service? → replace with the real collaborator.
  3. Request spec re-testing service branch logic already covered by the service's unit spec? → trim to wiring-only assertions.
  4. Factory with default associations nobody asked for? → make them explicit at the call site.

Keep looking

Skills are one crate of 607,533. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.