rails-testing
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 |
More from pproenca/dot-skills
zod
Zod schema validation best practices for type safety, parsing, and error handling. This skill should be used when defining z.object schemas, using z.string validations, safeParse, or z.infer. This skill does NOT cover React Hook Form integration patterns (use react-hook-form skill) or OpenAPI client generation (use orval skill).
2.0Kclean-architecture
Clean Architecture principles and best practices from Robert C. Martin's book. This skill should be used when designing software systems, reviewing code structure, or refactoring applications to achieve better separation of concerns. Triggers on tasks involving layers, boundaries, dependency direction, entities, use cases, or system architecture.
1.4Kemilkowal-animations
Emil Kowalski's animation best practices for web interfaces. Use when writing, reviewing, or implementing animations in React, CSS, or Framer Motion. Triggers on tasks involving transitions, easing, gestures, toasts, drawers, or motion.
917vitest
Vitest testing framework patterns for test setup, async testing, mocking with vi.*, snapshots, and test performance (formerly test-vitest). This skill should be used when writing or debugging Vitest tests. This skill does NOT cover TDD methodology (use test-tdd skill), API mocking with MSW (use test-msw skill), or Jest-specific APIs.
906typescript
This skill should be used when the user asks to "optimize TypeScript performance", "speed up tsc compilation", "configure tsconfig.json", "fix type errors", "improve async patterns", or encounters TS errors (TS2322, TS2339, "is not assignable to"). Also triggers on .ts, .tsx, .d.ts file work involving type definitions, module organization, or memory management. Does NOT cover TypeScript basics, framework-specific patterns, or testing.
820nuqs
nuqs (type-safe URL query state) best practices for Next.js applications. This skill should be used when writing, reviewing, or refactoring code that uses nuqs for URL state management. Triggers on tasks involving useQueryState, useQueryStates, search params, URL state, query parameters, nuqs parsers, or Next.js routing with state.
734