Write Rails tests with RSpec, FactoryBot, and mocking patterns. Use when creating tests, writing specs for models/controllers/requests, setting up test fixtures, or mocking external services.
Resources
5Install
npx skillscat add shivamsinghchahar/rails-skills/rails-testing-rspec Install via the SkillsCat registry.
Rails Testing with RSpec
Build comprehensive test suites using RSpec and FactoryBot. This skill covers spec structure, factories, mocking, and testing best practices.
Quick Start
Add to Gemfile:
group :test do
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'faker'
endGenerate RSpec install:
rails generate rspec:installWrite a test:
require 'rails_helper'
RSpec.describe User, type: :model do
describe 'validations' do
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_uniqueness_of(:email) }
end
describe '#full_name' do
it 'returns concatenated first and last name' do
user = build(:user, first_name: 'John', last_name: 'Doe')
expect(user.full_name).to eq('John Doe')
end
end
endRun tests:
rspec # All specs
rspec spec/models # Only model specs
rspec spec/models/user_spec.rb:10 # Specific lineCore Topics
RSpec Setup: See rspec-setup.md for configuration, describe blocks, contexts, and common matchers.
Factories: See factories-fixtures.md for FactoryBot setup, defining factories, and sequences.
Mocking: See mocking-stubbing.md for stubbing, mocking, spies, and testing external dependencies.
Patterns: See patterns.md for test organization, DRY specs, and common patterns.
Examples
See examples.md for:
- Model specs with validations and scopes
- Controller/request specs
- Integration tests
- Testing background jobs