> rails-testing
Ruby on Rails testing best practices for writing effective, maintainable test suites with RSpec. This skill should be used when writing, reviewing, or refactoring Rails tests to ensure proper test design, data management, and coverage patterns. Triggers on tasks involving RSpec specs, model tests, request specs, system tests, factory definitions, Capybara interactions, Sidekiq job tests, or test suite optimization. Complementary to rails-dev, ruby-optimise, and ruby-refactor skills.
curl "https://skillshub.wtf/pproenca/dot-skills/rails-testing?format=md"Community Ruby on Rails Testing Best Practices
Comprehensive testing guide for Ruby on Rails applications, maintained by Community. Contains 46 rules across 8 categories, prioritized by impact to guide automated test generation, review, and refactoring.
When to Apply
Reference these guidelines when:
- Writing new RSpec specs for models, requests, system tests, or jobs
- Setting up FactoryBot factories with traits and sequences
- Writing Capybara system tests for user journeys
- Testing background jobs with Sidekiq or Active Job
- Reviewing test code for anti-patterns (mystery guests, flaky tests, slow specs)
- Optimizing test suite performance and CI pipeline speed
- Organizing test files, shared examples, and custom matchers
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Test Design & Structure | CRITICAL | design- |
| 2 | Test Data Management | CRITICAL | data- |
| 3 | Model Testing | HIGH | model- |
| 4 | Request & Controller Testing | HIGH | request- |
| 5 | System & Acceptance Testing | MEDIUM-HIGH | system- |
| 6 | Async & Background Job Testing | MEDIUM | async- |
| 7 | Test Performance & Reliability | MEDIUM | perf- |
| 8 | Test Organization & Maintenance | LOW-MEDIUM | org- |
Quick Reference
1. Test Design & Structure (CRITICAL)
design-four-phase-test- Use four-phase test structure (setup, exercise, verify, teardown)design-behavior-over-implementation- Test observable behavior, not internal implementationdesign-one-assertion-per-test- One logical expectation per test for precise failure diagnosisdesign-descriptive-test-names- Write test names that read like specificationsdesign-avoid-mystery-guest- Make all test data visible within the test itselfdesign-avoid-conditional-logic- No if/else or loops in test codedesign-explicit-subject- Name subjects explicitly instead of using implicit subject
2. Test Data Management (CRITICAL)
data-factory-traits- Use composable factory traits instead of separate factoriesdata-minimal-attributes- Specify only attributes relevant to the testdata-build-over-create- Prefer build/build_stubbed over create when persistence isn't neededdata-avoid-fixture-coupling- Use factories instead of shared fixturesdata-transient-attributes- Use transient attributes for complex factory setupdata-sequence-unique-values- Use sequences for uniqueness-constrained fields
3. Model Testing (HIGH)
model-test-validations- Test validations with boundary cases, not just happy pathmodel-test-associations- Test associations explicitly including dependent behaviormodel-test-scopes- Test scopes with matching and non-matching recordsmodel-test-callbacks-sparingly- Test callback side effects, not callback existencemodel-test-custom-methods- Test public methods with input/output pairs across scenariosmodel-avoid-testing-framework- Don't test ActiveRecord or framework behaviormodel-test-enums- Test enum transitions and generated scopes
4. Request & Controller Testing (HIGH)
request-over-controller-specs- Use request specs over deprecated controller specsrequest-test-response-status- Assert HTTP status codes explicitlyrequest-test-authentication- Test authentication boundaries for every protected endpointrequest-test-authorization- Test authorization for each rolerequest-test-params-validation- Test parameter validation and edge casesrequest-json-response-structure- Assert JSON response structure for API endpoints
5. System & Acceptance Testing (MEDIUM-HIGH)
system-page-objects- Encapsulate page interactions in page objectssystem-use-accessible-selectors- Use accessible selectors over CSS/XPathsystem-avoid-sleep- Never use sleep — rely on Capybara's built-in waitingsystem-test-critical-paths- Reserve system tests for critical user journeyssystem-database-state- Use truncation strategy for system test database cleanupsystem-screenshot-on-failure- Capture screenshots on system test failure
6. Async & Background Job Testing (MEDIUM)
async-separate-enqueue-from-perform- Test enqueue and perform separatelyasync-use-fake-mode-default- Default to Sidekiq fake mode globallyasync-test-job-perform- Test job perform method directlyasync-test-mailer-delivery- Test mailer delivery with enqueued mail matcherasync-test-after-commit- Account for transaction-aware job enqueuing in Rails 7.2+
7. Test Performance & Reliability (MEDIUM)
perf-parallel-tests- Run tests in parallel across CPU coresperf-database-strategy- Use transaction strategy for non-system testsperf-profile-slow-specs- Profile and fix the slowest specsperf-quarantine-flaky-tests- Quarantine flaky tests instead of retryingperf-avoid-before-all-mutation- Never mutate state created in before(:all)
8. Test Organization & Maintenance (LOW-MEDIUM)
org-avoid-deep-nesting- Limit context nesting to 3 levelsorg-shared-examples-sparingly- Use shared examples only for true behavioral contractsorg-custom-matchers- Extract custom matchers for repeated domain assertionsorg-file-structure-mirrors-app- Mirror app directory structure in spec directory
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
Reference Files
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
> related_skills --same-repo
> valid-skill
A valid test skill with proper formatting. This skill should pass all validations and serves as a reference for the expected format.
> too-long-skill
This skill has more than 500 lines which should fail validation.
> missing-references
This skill references rules that do not have corresponding files in the references directory.
> missing-description
missing-description skill from pproenca/dot-skills