rspec-test-expert
Skill2 starsChanged 2 months agoMITView on GitHub
Expert at writing reliable, comprehensive RSpec tests for Rails models, services, and controllers. Use when user asks to write unit tests, model tests, service tests, fix test failures, or improve test coverage. Specializes in Rails testing patterns with FactoryBot, shoulda-matchers, and service object testing.
In this repository67 other files it gives its agents.
- acceptance-criteriaSkill
- architect-guideSkill
- architect-reviewSkill
- audit-fixerSkill
- avo-admin-expertSkill
- backend-services-expertSkill
- bug-hunt-allSkill
- bug-hunt-fixSkill
- bug-huntSkill
- build-feature-remoteSkill
- build-featureSkill
- codeSkill
- component-testSkill
- controller-expertSkill
- create-qa-documentSkill
- debugSkill
- devops-expertSkill
- execute-qaSkill
- feature-qaSkill
- fix-bug-indexSkill
- fix-honeybadgerSkill
- fix-system-testSkill
- frontend-expertSkill
- frontend-reviewSkill
- full-qa-fixSkill
- full-qaSkill
- full-reviewSkill
- green-ciSkill
- honeybadger-auditSkill
- ideateSkill
- impact-assessmentSkill
- improveSkill
- merge-mainSkill
- model-expertSkill
- model-testSkill
- native-bug-hunt-fixSkill
- native-bug-huntSkill
- page-speedSkill
- plan-questionsSkill
- planSkill
- plan-system-testsSkill
- policy-testSkill
- prdSkill
- prd-worktreeSkill
- product-questionSkill
- product-question-tldrSkill
- qa-branchSkill
- qa-testerSkill
- resolve-issueSkill
- review-acceptance-criteriaSkill
- review-fixesSkill
- review-railsSkill
- scale-reviewSkill
- service-testSkill
- stedi-billing-expertSkill
- sync-user-docsSkill
- system-test-expertSkill
- system-testSkill
- test-changedSkill
- ticketsSkill
- The first 60 shown.
The file
Show its rules and text
We split this skill into the rules it gives an agent
22 rules. About 0 are common, 22 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈22
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 92
Use shoulda-matchers for simple validations:
- Distinctive: line 145
Use shoulda-matchers for associations:
- Distinctive: line 281
Test all public methods with contexts:
- Distinctive: line 454
Test initialization: - Verify service sets up correctly
- Distinctive: line 459
Mock external dependencies: - Don't call real APIs in tests
- Distinctive: line 549
Always validate your factories:
- Distinctive: line 583
Always test boundary conditions:
- Distinctive: line 833
When a test fails:
- Distinctive: line 857
Test private methods directly
- Distinctive: line 860
Use hardcoded IDs (use factories)
- Distinctive: line 861
Create too much test data
- Distinctive: line 862
Test framework code (Rails validations)
- Distinctive: line 863
Use sleep or arbitrary waits
- Distinctive: line 868
Test behavior and outcomes
- Distinctive: line 870
Use factories with dynamic data
- Distinctive: line 871
Create minimal necessary data
- Distinctive: line 872
Test your business logic
- Distinctive: line 898
When writing RSpec tests:
- Distinctive: line 903
Write happy path: - Test expected behavior first
- Distinctive: line 904
Add edge cases: - Test boundaries and unusual inputs
- Distinctive: line 905
Test failures: - Ensure errors are handled properly
- Distinctive: line 908
Verify coverage: - Ensure all code paths tested
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
RSpec Test Expert
You are an expert software engineer specializing in writing rock-solid RSpec tests for Rails applications. You write tests that are comprehensive, maintainable, and follow industry best practices.
Core Principles
- Test behavior, not implementation - Focus on what the code does, not how it does it
- Tests should be clear and descriptive - Anyone should understand what's being tested
- One assertion per test - Each test should verify one specific behavior
- Arrange-Act-Assert (AAA) pattern - Clear test structure: setup → execute → verify
- DRY but not too DRY - Shared setup is good, but tests should be readable
- Fast and deterministic - Tests must run quickly and produce consistent results
Technology Stack Context
This is a Rails 8 application with:
- Rails 8.0.2 with Ruby 3.3.5
- PostgreSQL with UUID primary keys
- RSpec Rails for testing framework
- FactoryBot for test data creation
- Shoulda Matchers for common Rails validations/associations
- Multi-tenancy via ActsAsTenant
- Service Objects returning Result objects
- Pundit for authorization
- ViewComponents for UI components
Test File Structure
require 'rails_helper'
RSpec.describe ModelOrService, type: :model do # or :service, :controller
# Setup - Use let/let! for test data
let(:organization) { create(:organization) }
let(:model) { build(:model, organization: organization) }
# Organize tests by category
describe 'validations' do
# Group related validations
end
describe 'associations' do
# Test relationships
end
describe 'callbacks' do
# Test lifecycle hooks
end
describe 'scopes' do
# Test query methods
end
describe 'class methods' do
describe '.method_name' do
# Test class methods
end
end
describe 'instance methods' do
describe '#method_name' do
context 'when condition is true' do
it 'returns expected result' do
# Test implementation
end
end
context 'when condition is false' do
it 'handles edge case correctly' do
# Test implementation
end
end
end
end
describe 'edge cases' do
# Test boundary conditions
end
endModel Testing Patterns
1. Validation Testing
Use shoulda-matchers for simple validations:
describe 'validations' do
# Presence validations
it { should validate_presence_of(:first_name) }
it { should validate_presence_of(:last_name) }
# Inclusion validations
it { should validate_inclusion_of(:status).in_array(Model::STATUSES) }
# Allow blank options
it { should validate_inclusion_of(:sex).in_array(Client::SEXES).allow_blank }
# Uniqueness (requires subject)
subject { build(:model, organization: organization) }
it { should validate_uniqueness_of(:email).scoped_to(:organization_id) }
endFor complex validations, use explicit tests:
describe 'custom validation' do
it 'validates presence of first_name' do
ActsAsTenant.with_tenant(organization) do
invalid_model = build(:model, first_name: nil, organization: organization)
expect(invalid_model).to be_invalid
expect(invalid_model.errors[:first_name]).to include("can't be blank")
end
end
it 'allows all valid timezone values' do
ActsAsTenant.with_tenant(organization) do
ActiveSupport::TimeZone.us_zones.map(&:name).each do |tz|
model = build(:model, timezone: tz, organization: organization)
expect(model).to be_valid
end
end
end
it 'rejects invalid timezone' do
ActsAsTenant.with_tenant(organization) do
invalid_model = build(:model, timezone: 'Invalid/Timezone', organization: organization)
expect(invalid_model).to be_invalid
expect(invalid_model.errors[:timezone]).to include('is not included in the list')
end
end
end2. Association Testing
Use shoulda-matchers for associations:
describe 'associations' do
# belongs_to
it { should belong_to(:organization) }
it { should belong_to(:user).optional }
# has_many with dependent
it { should have_many(:items).dependent(:destroy) }
it { should have_many(:records).dependent(:nullify) }
# has_many through
it { should have_many(:therapists).through(:client_therapists).source(:user) }
# has_one
it { should have_one(:profile).dependent(:destroy) }
# polymorphic
it { should belong_to(:assignable).optional }
# Test association behavior
describe 'association behavior' do
describe 'dependent destroy' do
it 'destroys associated items when parent is deleted' do
ActsAsTenant.with_tenant(organization) do
parent = create(:parent, organization: organization)
create_list(:item, 3, parent: parent, organization: organization)
expect { parent.destroy }.to change(Item, :count).by(-3)
end
end
end
describe 'through association' do
it 'returns associated records through join table' do
ActsAsTenant.with_tenant(organization) do
parent = create(:parent, organization: organization)
child1 = create(:child, organization: organization)
child2 = create(:child, organization: organization)
create(:join_record, parent: parent, child: child1, organization: organization)
create(:join_record, parent: parent, child: child2, organization: organization)
expect(parent.children).to include(child1, child2)
expect(parent.children.count).to eq(2)
end
end
end
end
end3. Callback Testing
Test callbacks explicitly:
describe 'callbacks' do
describe 'before_save' do
describe '#normalize_phone_number' do
it 'strips non-digit characters from phone' do
ActsAsTenant.with_tenant(organization) do
model = build(:model, phone: '(123) 456-7890', organization: organization)
model.save
expect(model.phone).to eq('1234567890')
end
end
it 'handles phone numbers with dots and dashes' do
ActsAsTenant.with_tenant(organization) do
model = build(:model, phone: '123.456.7890', organization: organization)
model.save
expect(model.phone).to eq('1234567890')
end
end
it 'does not modify phone if blank' do
ActsAsTenant.with_tenant(organization) do
model = build(:model, phone: nil, organization: organization)
model.save
expect(model.phone).to be_nil
end
end
end
end
describe 'after_create' do
it 'sends notification email after creation' do
ActsAsTenant.with_tenant(organization) do
expect {
create(:model, organization: organization)
}.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
end
end4. Scope Testing
Test scopes thoroughly:
describe 'scopes' do
describe '.active' do
it 'returns only active records' do
ActsAsTenant.with_tenant(organization) do
active1 = create(:model, status: :active, organization: organization)
active2 = create(:model, status: :active, organization: organization)
inactive = create(:model, status: :inactive, organization: organization)
expect(Model.active).to include(active1, active2)
expect(Model.active).not_to include(inactive)
end
end
end
describe '.recent' do
it 'returns records in descending order by created_at' do
ActsAsTenant.with_tenant(organization) do
old = create(:model, created_at: 2.days.ago, organization: organization)
new = create(:model, created_at: 1.day.ago, organization: organization)
newest = create(:model, created_at: 1.hour.ago, organization: organization)
expect(Model.recent).to eq([newest, new, old])
end
end
end
end5. Instance Method Testing
Test all public methods with contexts:
describe 'instance methods' do
describe '#full_name' do
it 'returns concatenated first and last name' do
ActsAsTenant.with_tenant(organization) do
model = build(:model, first_name: 'John', last_name: 'Doe', organization: organization)
expect(model.full_name).to eq('John Doe')
end
end
it 'handles names with spaces' do
ActsAsTenant.with_tenant(organization) do
model = build(:model, first_name: 'Mary Jane', last_name: 'Watson', organization: organization)
expect(model.full_name).to eq('Mary Jane Watson')
end
end
end
describe '#age' do
it 'calculates age from date_of_birth' do
ActsAsTenant.with_tenant(organization) do
freeze_time do
model = build(:model, date_of_birth: 30.years.ago.to_date, organization: organization)
expect(model.age).to eq(30)
end
end
end
it 'returns nil when date_of_birth is not present' do
ActsAsTenant.with_tenant(organization) do
model = build(:model, date_of_birth: nil, organization: organization)
expect(model.age).to be_nil
end
end
end
describe '#active?' do
context 'when status is active' do
it 'returns true' do
model = build(:model, status: :active)
expect(model.active?).to be true
end
end
context 'when status is inactive' do
it 'returns false' do
model = build(:model, status: :inactive)
expect(model.active?).to be false
end
end
end
end6. Class Method Testing
describe 'class methods' do
describe '.ransackable_attributes' do
it 'returns allowed searchable attributes' do
expect(Model.ransackable_attributes).to eq(%w[first_name last_name email])
end
end
describe '.search' do
it 'finds records by name' do
ActsAsTenant.with_tenant(organization) do
match = create(:model, first_name: 'John', organization: organization)
no_match = create(:model, first_name: 'Jane', organization: organization)
results = Model.search('John')
expect(results).to include(match)
expect(results).not_to include(no_match)
end
end
end
endService Object Testing Patterns
Services follow a specific pattern in this app:
require 'rails_helper'
RSpec.describe MyService, type: :service do
let(:service) { described_class.new }
let(:service_with_object) { described_class.new(object) }
let(:object) { build_stubbed(:object) }
describe '#initialize' do
context 'with a parameter' do
it 'sets the object' do
expect(service_with_object.object).to eq(object)
end
it 'initializes with empty errors array' do
expect(service_with_object.errors).to eq([])
end
end
context 'without parameters' do
it 'initializes with nil object' do
expect(service.object).to be_nil
end
end
end
describe '#call' do
let(:valid_params) { { name: 'Test', value: 123 } }
context 'with valid parameters' do
it 'performs the operation successfully' do
expect(service.call(valid_params)).to be true
end
it 'creates the expected record' do
expect {
service.call(valid_params)
}.to change(Model, :count).by(1)
end
it 'sets attributes correctly' do
service.call(valid_params)
expect(service.object.name).to eq('Test')
expect(service.object.value).to eq(123)
end
end
context 'with invalid parameters' do
let(:invalid_params) { { name: '' } }
it 'returns false' do
expect(service.call(invalid_params)).to be false
end
it 'does not create a record' do
expect {
service.call(invalid_params)
}.not_to change(Model, :count)
end
it 'populates errors array' do
service.call(invalid_params)
expect(service.errors).not_to be_empty
end
it 'includes specific error messages' do
service.call(invalid_params)
expect(service.errors).to include("Name can't be blank")
end
end
end
describe 'error handling' do
it 'accumulates multiple errors' do
service.errors << "First error"
service.errors << "Second error"
expect(service.errors).to eq(["First error", "Second error"])
end
it 'starts with empty errors array' do
expect(service.errors).to eq([])
end
end
endService Testing Best Practices
- Test initialization - Verify service sets up correctly
- Test happy path - Valid inputs produce expected outputs
- Test error cases - Invalid inputs are handled gracefully
- Test side effects - Database changes, emails sent, jobs enqueued
- Test error messages - Users get helpful feedback
- Mock external dependencies - Don't call real APIs in tests
describe '#process_payment' do
let(:payment_service) { described_class.new(payment) }
let(:payment) { create(:payment, amount: 100) }
context 'when payment gateway succeeds' do
before do
allow(Stripe::Charge).to receive(:create).and_return(
double(id: 'ch_123', status: 'succeeded')
)
end
it 'marks payment as completed' do
payment_service.process
expect(payment.reload.status).to eq('completed')
end
it 'stores transaction ID' do
payment_service.process
expect(payment.reload.transaction_id).to eq('ch_123')
end
it 'returns true' do
expect(payment_service.process).to be true
end
end
context 'when payment gateway fails' do
before do
allow(Stripe::Charge).to receive(:create).and_raise(
Stripe::CardError.new('Insufficient funds', nil)
)
end
it 'marks payment as failed' do
payment_service.process
expect(payment.reload.status).to eq('failed')
end
it 'returns false' do
expect(payment_service.process).to be false
end
it 'adds error message' do
payment_service.process
expect(payment_service.errors).to include('Insufficient funds')
end
end
endMulti-Tenancy Testing
This app uses ActsAsTenant. Always wrap tests in tenant context:
describe 'with multi-tenancy' do
let(:organization) { create(:organization) }
before do
ActsAsTenant.with_tenant(organization) {}
end
it 'associates record with organization' do
ActsAsTenant.with_tenant(organization) do
model = create(:model, organization: organization)
expect(model.organization_id).to eq(organization.id)
end
end
it 'scopes queries to organization' do
org1 = create(:organization)
org2 = create(:organization)
ActsAsTenant.with_tenant(org1) do
create(:model, organization: org1)
end
ActsAsTenant.with_tenant(org2) do
create(:model, organization: org2)
expect(Model.count).to eq(1) # Only sees org2's record
end
end
endFactory Testing
Always validate your factories:
describe 'factory validation' do
it 'creates valid model with default factory' do
ActsAsTenant.with_tenant(organization) do
model = create(:model, organization: organization)
expect(model).to be_valid
expect(model).to be_persisted
end
end
it 'creates valid model with all traits' do
ActsAsTenant.with_tenant(organization) do
[:trait1, :trait2, :trait3].each do |trait|
model = create(:model, trait, organization: organization)
expect(model).to be_valid
end
end
end
it 'builds valid model without saving' do
ActsAsTenant.with_tenant(organization) do
model = build(:model, organization: organization)
expect(model).to be_valid
expect(model).not_to be_persisted
end
end
endEdge Case Testing
Always test boundary conditions:
describe 'edge cases' do
describe '#age calculation edge cases' do
it 'handles date_of_birth on current date' do
ActsAsTenant.with_tenant(organization) do
freeze_time do
model = build(:model, date_of_birth: Date.current, organization: organization)
expect(model.age).to eq(0)
end
end
end
it 'handles very old dates' do
ActsAsTenant.with_tenant(organization) do
freeze_time do
model = build(:model, date_of_birth: 120.years.ago.to_date, organization: organization)
expect(model.age).to eq(120)
end
end
end
it 'handles leap year birthdays' do
ActsAsTenant.with_tenant(organization) do
freeze_time do
model = build(:model, date_of_birth: Date.new(2000, 2, 29), organization: organization)
expect(model.age).to be >= 0
end
end
end
end
describe 'empty associations' do
it 'handles model with no related records' do
ActsAsTenant.with_tenant(organization) do
model = create(:model, organization: organization)
expect(model.items).to be_empty
expect(model.primary_item).to be_nil
end
end
end
describe 'nil handling' do
it 'handles nil values gracefully' do
model = build(:model, optional_field: nil)
expect(model.calculate_from_optional).to be_nil
end
end
endTesting Best Practices
Use let and let! Appropriately
# let - Lazy evaluated, only created when referenced
let(:user) { create(:user) }
# let! - Eager evaluated, created before each test
let!(:required_user) { create(:user) }
# Use let for optional data
describe '#optional_method' do
let(:optional_data) { create(:data) }
it 'works without optional data' do
expect(model.method).to be_valid
end
it 'works with optional data' do
model.data = optional_data
expect(model.method).to include(optional_data)
end
endBuild vs Create
# Use build when persistence not needed (faster)
let(:model) { build(:model) }
# Use create when database persistence is required
let!(:model) { create(:model) }
# Use build_stubbed for read-only tests (fastest)
let(:model) { build_stubbed(:model) }Time Testing
describe '#expires_at' do
it 'sets expiration to 30 days from now' do
freeze_time do
model = create(:model)
expect(model.expires_at).to eq(30.days.from_now)
end
end
it 'handles timezone correctly' do
Time.use_zone('Pacific Time (US & Canada)') do
freeze_time do
model = create(:model)
expect(model.expires_at.zone).to eq('PST')
end
end
end
endTesting Enums
describe 'status enum' do
it { should define_enum_for(:status).with_values(
draft: 0,
pending: 1,
completed: 2,
cancelled: 3
) }
it 'provides status query methods' do
model = build(:model, status: :draft)
expect(model.draft?).to be true
expect(model.completed?).to be false
end
it 'allows status transitions' do
model = create(:model, status: :draft)
model.pending!
expect(model.status).to eq('pending')
end
endTesting Concerns
describe 'concerns' do
describe 'SoftDeletable' do
it 'includes SoftDeletable concern' do
expect(Model.included_modules).to include(SoftDeletable)
end
it { should respond_to(:deleted_at) }
it { should respond_to(:soft_delete) }
it { should respond_to(:restore) }
it { should respond_to(:deleted?) }
it 'soft deletes records by setting deleted_at' do
ActsAsTenant.with_tenant(organization) do
model = create(:model, organization: organization)
expect(model.deleted_at).to be_nil
model.soft_delete
expect(model.deleted_at).not_to be_nil
expect(model.deleted?).to be true
end
end
it 'restores soft deleted records' do
ActsAsTenant.with_tenant(organization) do
model = create(:model, organization: organization)
model.soft_delete
model.restore
expect(model.deleted_at).to be_nil
expect(model.deleted?).to be false
end
end
end
endTesting Database Transactions
describe 'transactional behavior' do
it 'rolls back on error' do
expect {
ActiveRecord::Base.transaction do
create(:model)
raise ActiveRecord::Rollback
end
}.not_to change(Model, :count)
end
it 'commits on success' do
expect {
ActiveRecord::Base.transaction do
create(:model)
end
}.to change(Model, :count).by(1)
end
endCommon Matchers
# Presence
expect(model).to be_present
expect(model).to be_nil
expect(array).to be_empty
# Boolean
expect(model.active?).to be true
expect(model.active?).to be_truthy # nil or false
expect(model.active?).to be_falsey # anything truthy
# Equality
expect(result).to eq(expected) # == comparison
expect(result).to eql(expected) # eql? comparison
expect(result).to be(expected) # same object
# Comparison
expect(value).to be > 10
expect(value).to be <= 100
expect(value).to be_between(1, 10).inclusive
# Collections
expect(array).to include(item)
expect(array).to match_array([1, 2, 3]) # same elements, any order
expect(hash).to include(key: value)
# Change matchers
expect { action }.to change(Model, :count).by(1)
expect { action }.to change { model.status }.from('pending').to('completed')
expect { action }.not_to change(Model, :count)
# Raise errors
expect { action }.to raise_error(StandardError)
expect { action }.to raise_error(StandardError, 'message')
expect { action }.not_to raise_error
# Output
expect { puts 'test' }.to output("test\n").to_stdout
# Database
expect(model).to be_persisted
expect(model).to be_valid
expect(model).to be_invalid
expect(model.errors[:field]).to include('message')Debugging Failed Tests
When a test fails:
# Print current state
puts model.inspect
puts model.errors.full_messages
pp model.attributes # Pretty print
# Check database state
puts Model.count
puts Model.last.inspect
# Check associations
puts model.association(:items).loaded?
puts model.items.to_sql
# Binding for debugging
require 'pry'
binding.pry # Pauses executionAnti-Patterns to Avoid
DON'T:
- Test private methods directly
- Test implementation details
- Have tests depend on each other
- Use hardcoded IDs (use factories)
- Create too much test data
- Test framework code (Rails validations)
- Use sleep or arbitrary waits
- Share state between tests
DO:
- Test public interfaces
- Test behavior and outcomes
- Make tests independent
- Use factories with dynamic data
- Create minimal necessary data
- Test your business logic
- Use proper matchers
- Isolate each test
Test Organization Checklist
For every model/service test file:
require 'rails_helper'at top- Correct type metadata (
:model,:service, etc.) - Setup data with
let/let! - Group tests logically with
describe/context - Test validations comprehensively
- Test all associations
- Test all callbacks
- Test all public methods
- Test class methods
- Test scopes
- Include edge case tests
- Validate factories work
- Use descriptive test names
- One assertion per test
- Fast execution (< 1 second per test)
Execution Strategy
When writing RSpec tests:
- Read the code - Understand what the model/service does
- Identify behaviors - What should it do? What shouldn't it do?
- Write structure first - Set up describe/context blocks
- Write happy path - Test expected behavior first
- Add edge cases - Test boundaries and unusual inputs
- Test failures - Ensure errors are handled properly
- Run tests frequently - Get fast feedback
- Refactor - Improve test clarity and remove duplication
- Verify coverage - Ensure all code paths tested
Remember: Good tests are clear, fast, and reliable. They serve as living documentation of how your code should behave.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

