$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
Ruby on RailsintermediateNew

Rails Testing

Share

Write RSpec tests with factories and mocks

Works with OpenClaude

You 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 rspec to verify rspec-rails, rspec-core, and rspec-mocks are installed
  • Check spec/rails_helper.rb exists and contains require 'rspec/rails'
  • Run bundle list | grep factory_bot to confirm factory_bot-rails is in your Gemfile

Steps

  1. Create a factory file at spec/factories/users.rb using FactoryBot.define with traits for different user states
  2. Define model factories with associations using association :model_name syntax
  3. Write a test file at spec/models/user_spec.rb with describe User do block structure
  4. Use let(:user) { create(:user) } to instantiate factory objects in before hooks
  5. Mock external service calls with allow(ExternalService).to receive(:method_name).and_return(value)
  6. Use expect(Model).to have_received(:method).with(arguments) after mocking to verify method calls
  7. Test validations with expect(user).to validate_presence_of(:email) using shoulda-matchers
  8. Use instance_double or class_double for 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

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
railsrspectesting

Install command:

curl -o ~/.claude/skills/rails-testing.md https://clskills.in/skills/rails/rails-testing.md

Related Ruby on Rails Skills

Other Claude Code skills in the same category — free to download.

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.