RSpec Testing Patterns

Define RSpec testing patterns for Rails unit, integration, and system tests.

10|1|Updated Dec 20, 2025
One-click install
npx skills add https://github.com/Kaakati/rails-enterprise-dev --skill rspec-testing-patterns
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: RSpec Testing Patterns
Source: https://github.com/Kaakati/rails-enterprise-dev/tree/main/plugins/rails-enterprise-dev/skills/rspec-testing-patterns
Command: npx skills add https://github.com/Kaakati/rails-enterprise-dev --skill rspec-testing-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

A comprehensive guide to testing Rails apps with RSpec, including factories, shared examples, mocks, and component tests.

Core Features & Use Cases

  • Directory structure for specs
  • Basic spec structures for models, services, controllers
  • Factories (FactoryBot) and shared examples
  • Testing ViewComponents and background jobs

Quick Start

Create a new spec file for a model and write a basic test to assert a validation.

Frequently Asked Questions about RSpec Testing Patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write maintainable RSpec tests for Rails models?▼

RSpec testing patterns for Rails provide conventions for structuring model specs with clear validation and behavior assertions. Organize specs in a `spec/models` directory, use descriptive context blocks, and apply shared examples to reduce duplication across similar test cases.

What's the best way to set up test data with factories in RSpec?▼

FactoryBot factories define reusable test data templates for Rails specs. Create factories in `spec/factories`, define traits for variations, and use them in tests instead of fixtures to keep data setup explicit and maintainable across unit, integration, and system tests.

How do I test Rails controllers and requests with RSpec?▼

RSpec testing patterns cover controller and request specs with standardized directory structure (`spec/controllers`, `spec/requests`), parameter passing, and response assertions. Use shared contexts for common setup to maintain consistency across endpoint tests.

Can I test ViewComponents and background jobs with RSpec?▼

Yes, RSpec testing patterns extend to ViewComponents and background jobs in Rails. Create spec files in `spec/components` and `spec/jobs`, apply the same mocking and assertion strategies, and use shared examples for recurring test logic across component and job suites.

What mocking strategies should I use in Rails RSpec tests?▼

RSpec testing patterns specify mocking conventions for Rails specs to isolate units under test. Apply mocks for external dependencies, database calls, and API interactions; use shared examples to enforce consistent mock behavior across related specs in models, services, and controllers.

How do I organize shared examples and contexts in RSpec specs?▼

Shared examples and contexts reduce duplication in RSpec test suites by defining reusable behavior blocks. Define them in `spec/support`, name them descriptively by their shared responsibility, and include them in model, controller, and service specs to enforce consistent testing conventions across Rails projects.