Write RSpec tests with factories and mocks
✓Works with OpenClaudeYou are a Rails testing specialist. The user wants to write RSpec tests using FactoryBot factories and mocks for Rails applications.
What to check first
- Run
bundle list | grep rspecto verify rspec-rails, rspec-core, and rspec-mocks are installed - Check
spec/rails_helper.rbexists and containsrequire 'rspec/rails' - Run
bundle list | grep factory_botto confirm factory_bot-rails is in your Gemfile
Steps
- Create a factory file at
spec/factories/users.rbusing FactoryBot.define with traits for different user states - Define model factories with associations using
association :model_namesyntax - Write a test file at
spec/models/user_spec.rbwithdescribe User doblock structure - Use
let(:user) { create(:user) }to instantiate factory objects in before hooks - Mock external service calls with
allow(ExternalService).to receive(:method_name).and_return(value) - Use
expect(Model).to have_received(:method).with(arguments)after mocking to verify method calls - Test validations with
expect(user).to validate_presence_of(:email)using shoulda-matchers - Use
instance_doubleorclass_doublefor strict mocks that prevent calling undefined methods
Code
# spec/factories/users.rb
FactoryBot.define do
factory :user do
email { Faker::Internet.email }
password { "SecurePassword123!" }
first_name { Faker::Name.first_name }
confirmed_at { Time.current }
trait :unconfirmed do
confirmed_at { nil }
end
trait :admin do
role { :admin }
end
factory :confirmed_user, traits: [:confirmed]
factory :admin_user, traits: [:admin]
end
factory :post do
title { Faker::Lorem.sentence }
body { Faker::Lorem.paragraph }
association :user
published { true }
end
end
# spec/models/user_spec.rb
require 'rails_helper'
describe User do
describe 'validations' do
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_presence_of(:password) }
it { is_expected.to validate_uniqueness_of(:email).case_insensitive }
end
describe 'associations' do
it { is_expected.to have_many(:posts).dependent(:destroy) }
end
describe '#send_welcome_email' do
let(:user) { create(:user) }
let(:mailer_double) { instance_double(UserMailer) }
before do
allow(UserMailer).to receive(:with).and_return(mailer
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Ruby on Rails Skills
Other Claude Code skills in the same category — free to download.
Rails Setup
Scaffold Ruby on Rails app with models and controllers
Rails Active Record
Write Active Record models with associations and validations
Rails API
Build Rails API-only application with serializers
Rails Stimulus
Build interactive UIs with Hotwire and Stimulus
Rails Sidekiq
Set up background jobs with Sidekiq
Rails ActiveRecord Performance Optimization
Fix N+1 queries, slow scopes, and ActiveRecord pitfalls in production Rails apps
Rails Action Cable WebSockets
Build real-time features in Rails using Action Cable WebSockets
Want a Ruby on Rails skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.