Skip to content
rails-testing logo

Rails Testing

rails-testing

Specialized skill for Rails testing with RSpec, FactoryBot, and TDD workflows. Use when writing tests, setting up test coverage, creating factories, or implementing test-driven development. Covers unit tests, integration tests, system tests, and component tests.

SKILL.md

Full skill instructions

Rails Testing

Professional Rails testing guide following TDD best practices with RSpec.

When to Use This Skill

  • Writing RSpec tests (model, controller, request, system, component)
  • Implementing test-driven development (TDD) workflow
  • Creating FactoryBot factories and traits
  • Setting up test coverage with SimpleCov
  • Writing integration and system tests with Capybara
  • Testing ViewComponents
  • Testing background jobs
  • Testing interactions and business logic

Test-First Development (NON-NEGOTIABLE)

Every feature MUST follow TDD cycle:

# 1. Write failing test FIRST
RSpec.describe User do
  it "validates presence of email" do
    user = User.new(email: nil)
    expect(user).not_to be_valid
  end
end

# 2. Run test - MUST fail (Red)
# 3. Implement minimal code to pass (Green)
# 4. Refactor while keeping tests green (Refactor)

Strict Rules:

  • Tests written BEFORE implementation
  • Never commit failing tests to main branch
  • Never skip/​disable tests to make builds pass
  • Red-Green-Refactor cycle strictly enforced
  • Tests serve as living documentation

Test Coverage Expectations

LayerCoverage TargetWhat to Test
Models100%All validations, scopes, methods
Interactions100%All business logic paths
Components90%+Rendering, conditional logic
Controllers80%+Success/​failure paths, auth
System TestsKey flowsCritical user journeys

Testing Tools

# Gemfile
group :development, :test do
  gem "rspec-rails"        # Testing framework
  gem "factory_bot_rails"  # Test data factories
  gem "faker"              # Fake data generation
  gem "shoulda-matchers"   # One-liner matchers (optional)
end

group :test do
  gem "capybara"           # System test DSL
  gem "selenium-webdriver" # Browser automation
  gem "database_cleaner-active_record"  # Test DB cleanup
  gem "simplecov", require: false       # Coverage reporting
end

Quick Reference

Model Testing

RSpec.describe Article, type: :model do
  describe "associations" do
    it { is_expected.to belong_to(:user) }
    it { is_expected.to have_many(:comments).dependent(:destroy) }
  end

  describe "validations" do
    it { is_expected.to validate_presence_of(:title) }
    it { is_expected.to validate_uniqueness_of(:slug) }
  end

  describe "scopes" do
    let!(:published) { create(:article, :published) }
    let!(:draft) { create(:article, :draft) }

    it "returns only published" do
      expect(Article.published).to contain_exactly(published)
    end
  end
end

Interaction Testing

RSpec.describe Users::Create, type: :interaction do
  let(:valid_params) { { email: "[email protected]", name: "John" } }

  context "with valid parameters" do
    it "creates user" do
      expect { described_class.run(valid_params) }
        .to change(User, :count).by(1)
    end

    it "returns valid outcome" do
      outcome = described_class.run(valid_params)
      expect(outcome).to be_valid
    end
  end
end

System Testing

RSpec.describe "Article Management", type: :system do
  let(:user) { create(:user) }

  before do
    driven_by(:selenium_chrome_headless)
    login_as(user)
  end

  it "allows creating article" do
    visit new_article_path

    fill_in "Title", with: "My Article"
    fill_in "Body", with: "Content"
    click_on "Create Article"

    expect(page).to have_content("Article was successfully created")
  end
end

Factory Pattern

FactoryBot.define do
  factory :user do
    sequence(:email) { |n| "user#{n}@example.com" }
    name { Faker::Name.name }

    trait :admin do
      role { "admin" }
    end

    trait :with_articles do
      after(:create) do |user|
        create_list(:article, 3, user: user)
      end
    end
  end
end

Running Tests

# All tests
bundle exec rspec

# Specific file
bundle exec rspec spec/​models/​user_spec.rb

# Specific test by line number
bundle exec rspec spec/​models/​user_spec.rb:15

# With coverage
COVERAGE=true bundle exec rspec

# Watch mode (requires guard gem)
bundle exec guard

Performance Testing

N+1 Query Detection with Bullet

# Gemfile
group :development, :test do
  gem 'bullet'
end

# config/​environments/​test.rb
config.after_initialize do
  Bullet.enable = true
  Bullet.bullet_logger = true
  Bullet.raise = true  # Raise error in tests
end

# RSpec test will fail if N+1 detected
RSpec.describe "Articles", type: :request do
  it "avoids N+1 queries" do
    create_list(:article, 5, :with_author)

    get articles_path

    # Bullet will raise if N+1 detected
    expect(response).to have_http_status(:ok)
  end
end

Performance Profiling in Tests

# spec/​support/​performance_helper.rb
RSpec.configure do |config|
  config.around(:each, :performance) do |example|
    start = Time.now
    example.run
    duration = Time.now - start

    puts "\n#{example.metadata[:full_description]}: #{duration.round(3)}s"
    expect(duration).to be < 0.5  # Fail if > 500ms
  end
end

# Usage in spec
RSpec.describe "Dashboard", :performance do
  it "loads quickly" do
    # Test code
  end
end

Benchmarking

# Gemfile
gem 'benchmark-ips'

# spec/​benchmarks/​query_benchmark.rb
require 'benchmark/​ips'

Benchmark.ips do |x|
  x.report("without includes") { Article.all.map(&:author) }
  x.report("with includes") { Article.includes(:author).map(&:author) }
  x.compare!
end

Reference Documentation

For comprehensive testing standards, refer to:

  • Full testing guide: testing.md (detailed examples and advanced patterns)

More skills from boisenoise

database-cloud-optimization-cost-optimize logo
boisenoise/skills-collections

database-cloud-optimization-cost-optimize

You are a cloud cost optimization expert specializing in reducing infrastructure expenses while maintaining performance and reliability. Analyze cloud spending, identify savings opportunities, and implement cost-effective architectures across AWS, Azure, and GCP.

10 0
View
postmark-automation logo
boisenoise/skills-collections

postmark-automation

Automate Postmark email delivery tasks via Rube MCP (Composio): send templated emails, manage templates, monitor delivery stats and bounces. Always search tools first for current schemas.

10 0
View
game-development logo
boisenoise/skills-collections

game-development

Game development orchestrator. Routes to platform-specific skills based on project needs.

10 0
View
test-fixing logo
boisenoise/skills-collections

test-fixing

Systematically identify and fix all failing tests using smart grouping strategies. Use when explicitly asks to fix tests (\"fix these tests\", \"make tests pass\"), reports test failures (\"tests are failing\", \"test suite is broken\"), or completes implementation and wants tests passing.

10 0
View
notebooklm logo
boisenoise/skills-collections

notebooklm

Interact with Google NotebookLM to query documentation with Gemini's source-grounded answers. Each question opens a fresh browser session, retrieves the answer exclusively from your uploaded documents, and closes.

10 0
View
agent-evaluation logo
boisenoise/skills-collections

agent-evaluation

Testing and benchmarking LLM agents including behavioral testing,

10 0
View
clickup-automation logo
boisenoise/skills-collections

clickup-automation

Automate ClickUp project management including tasks, spaces, folders, lists, comments, and team operations via Rube MCP (Composio). Always search tools first for current schemas.

10 0
View
wordpress-penetration-testing logo
boisenoise/skills-collections

wordpress-penetration-testing

Assess WordPress installations for common vulnerabilities and WordPress 7.0 attack surfaces.

10 0
View
ln-761-secret-scanner logo
boisenoise/skills-collections

ln-761-secret-scanner

Scans codebase for hardcoded secrets with severity classification and remediation guidance. Use when auditing a project for leaked credentials.

10 0
View
azure-monitor-ingestion-java logo
boisenoise/skills-collections

azure-monitor-ingestion-java

Azure Monitor Ingestion SDK for Java. Send custom logs to Azure Monitor via Data Collection Rules (DCR) and Data Collection Endpoints (DCE).

10 0
View
ln-513-agent-reviewer logo
boisenoise/skills-collections

ln-513-agent-reviewer

Runs parallel external agent reviews (Codex + Gemini) on code changes. Process-as-arrive, critical verification with debate. Returns filtered suggestions.

10 0
View
drip-jobs-automation logo
boisenoise/skills-collections

drip-jobs-automation

Automate Drip Jobs tasks via Rube MCP (Composio). Always search tools first for current schemas.

10 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View