shivamsinghchahar

rails-testing-rspec

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.

shivamsinghchahar 0 Updated 3mo ago

Resources

5
GitHub

Install

npx skillscat add shivamsinghchahar/rails-skills/rails-testing-rspec

Install via the SkillsCat registry.

SKILL.md

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'
end

Generate RSpec install:

rails generate rspec:install

Write 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
end

Run tests:

rspec                    # All specs
rspec spec/models       # Only model specs
rspec spec/models/user_spec.rb:10  # Specific line

Core 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