Skip to content
Back to skills

Rspec

ASecurity

RSpec testing best practices for Ruby and Rails applications, covering test organization, data management, and isolation patterns.

  • 248 stars
  • 0 votes
  • 0 copies
  • 6 views
  • Added February 10, 2026
developmentrubyrailstestingapidatabase

Works with

  • api

Security analysis

A100/100

Scanned February 12, 2026

npx -y skills add Mindrally/skills --skill rspec --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Rspec?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Rspec
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mindrally-rspec/badge)](https://www.skillsdirectory.com/skills/mindrally-rspec)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: rspec
description: RSpec testing best practices for Ruby and Rails applications, covering test organization, data management, and isolation patterns.
---

# RSpec Testing Best Practices

You are an expert in Ruby, Rails, and RSpec testing.

## Key Principles

### Comprehensive Coverage
Tests must cover both typical cases and edge cases, including invalid inputs and error conditions.

### Readability and Clarity
- Employ descriptive names for `describe`, `context`, and `it` blocks
- Use the `expect` syntax for improved assertion readability
- Keep test code concise without unnecessary complexity
- Include comments explaining complex logic

### Test Organization
- Use `describe` for classes/modules and `context` for different scenarios
- Use the `subject` helper to prevent repetition when defining objects under test
- Mirror your source file structure within the spec directory

### Test Data Management
- Leverage `let` and `let!` for minimal, necessary setup
- Prefer FactoryBot factories over fixtures for generating test data
- Create only the data necessary for each test

### Test Isolation
- Each test must be independent without shared state between tests
- Mock external services (APIs, databases) and stub methods appropriately
- Avoid over-mocking: test real behavior when feasible

### Reduce Duplication
- Share common behaviors across contexts using `shared_examples`
- Extract repetitive patterns into helpers or custom matchers
- Use `shared_context` for common setup across multiple specs

## Example Structure

```ruby
RSpec.describe User, type: :model do
  subject { build(:user) }

  describe 'validations' do
    it { is_expected.to validate_presence_of(:email) }
    it { is_expected.to validate_uniqueness_of(:email) }
  end

  describe '#full_name' do
    context 'when both first and last name are present' do
      let(:user) { build(:user, first_name: 'John', last_name: 'Doe') }

      it 'returns the combined name' do
        expect(user.full_name).to eq('John Doe')
      end
    end

    context 'when last name is missing' do
      let(:user) { build(:user, first_name: 'John', last_name: nil) }

      it 'returns only the first name' do
        expect(user.full_name).to eq('John')
      end
    end
  end
end
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.