rspec

Guide writing RSpec tests for Ruby and Rails applications.

12|2|Updated Mar 11, 2025
One-click install
npx skills add https://github.com/el-feo/ai-context --skill rspec
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rspec
Source: https://github.com/el-feo/ai-context/tree/main/skills/rspec
Command: npx skills add https://github.com/el-feo/ai-context --skill rspec

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill guides you in writing comprehensive tests in RSpec for Ruby and Rails applications, covering models, requests, system specs, factories, mocks, and TDD workflows.

Core Features & Use Cases

  • Model specs, request specs, and system specs patterns with clear examples.
  • FactoryBot integration and realistic test data strategies.
  • Deep guidance on mocks, doubles, spies, and matchers for robust tests.

Quick Start

Create a basic model spec and run the test suite with bundle exec rspec to validate behavior.

Frequently Asked Questions about rspec

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

FAQPage Schema
How do I write model specs in RSpec for Rails applications?

Model specs in RSpec test your application logic and validations. Write specs that verify model behavior, associations, and validations by creating instances, calling methods, and asserting outcomes using RSpec matchers like `expect()` and `be_valid`.

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

FactoryBot generates realistic test data efficiently. Define factories with attributes matching your models, then use `create()` or `build()` in specs to generate instances without manual setup, reducing duplication and improving maintainability.

How do I use mocks and doubles to isolate units in RSpec tests?

Mocks and doubles in RSpec replace external dependencies and verify interactions. Use `double()`, `spy()`, and `allow()` to stub methods, test behavior in isolation, and assert that collaborators receive expected calls without hitting the database or external services.

Can I test Rails request and system behavior with RSpec?

RSpec supports request specs for API endpoints and system specs for full user workflows. Request specs test routing and controller logic; system specs simulate browser interactions with Capybara, covering the full request cycle.

What commands run my RSpec test suite in a Rails project?

Run `bundle exec rspec` to execute all specs, `bundle exec rspec spec/models` for a specific directory, or `bundle exec rspec path/to/spec.rb:10` for a single test at line 10, validating behavior against your test suite.

How do I apply TDD workflows when writing RSpec tests?

TDD with RSpec follows red-green-refactor: write a failing spec, implement the minimum code to pass it, then refactor. This cycle ensures tests drive design, reduce bugs, and keep code maintainable from the start.