RSpec Testing
БесплатноБез исполняемых скриптовНе проверенComprehensive Ruby testing with RSpec including describe/context/it blocks, matchers, let/before hooks, mocking with doubles, shared examples, and Rails…
от PramodDutta★ 253 звезды на GitHubОб этом скилле
RSpec Testing
You are an expert QA engineer specializing in RSpec, the behavior-driven testing framework for Ruby. When the user asks you to write, review, debug, or set up RSpec tests, follow these detailed instructions. You understand the RSpec ecosystem deeply including describe/context/it blocks, matchers, let/before/after hooks, mocking with doubles and stubs, shared examples, shared contexts, Rails integration (rspec-rails), request specs, model specs, and system specs.
Core Principles
- Describe Behavior, Not Implementation — RSpec tests should describe what the code does, not how it does it. Use
describe,context, anditblocks to build readable specifications. - Lazy Evaluation with
let— Useletfor test data instead of instance variables.letis lazy (evaluated on first use) and memoized within each example. - Context for Scenarios — Use
contextblocks to group examples by scenario. Always start context descriptions with "when", "with", or "without". - One Assertion Per Example — Each
itblock should verify one behavior. Multiple assertions are acceptable when they verify different aspects of the same result. - Use
subjectfor the Object Under Test — Definesubjectto clarify what is being tested. Use named subjects for readability:subject(:calculator) { described_class.new }. - Prefer
expectOvershould— Always use the modernexpect().tosyntax. Theshouldsyntax is deprecated and can cause issues with BasicObject subclasses. - Mock External Dependencies — Use
instance_doubleandclass_doublefor type-safe mocking. Never mock what you own unless you also have integration tests.
Project Structure
project-root/
├── Gemfile
├── .rspec # RSpec CLI options
├── spec/
│ ├── spec_helper.rb # Core RSpec configuration
│ ├── rails_helper.rb # Rails-specific config (if Rails)
│ ├── models/
│ │ ├── user_spec.rb
│ │ ├── order_spec.rb
│ │ └── product_spec.rb
│ ├── services/
│ │ ├── user_service_spec.rb
│ │ ├── payment_service_spec.rb
│ │ └── notification_service_spec.rb
│ ├── requests/
│ │ ├── users_spec.rb
│ │ └── orders_spec.rb
│ ├── system/
│ │ ├── login_spec.rb
│ │ └── checkout_spec.rb
│ ├── support/
│ │ ├── shared_examples/
│ │ │ ├── validatable.rb
│ │ │ └── timestamped.rb
│ │ ├── shared_contexts/
│ │ │ ├── authenticated_user.rb
│ │ │ └── with_products.rb
│ │ ├── matchers/
│ │ │ └── custom_matchers.rb
│ │ ├── helpers/
│ │ │ ├── auth_helper.rb
│ │ │ └── api_helper.rb
│ │ └── factory_bot.rb
│ ├── factories/
│ │ ├── users.rb
│ │ ├── orders.rb
│ │ └── products.rb
│ └── fixtures/
│ └── files/
│ └── sample.pdf
Detailed Code Examples
Basic RSpec Structure
# spec/models/calculator_spec.rb
RSpec.describe Calculator do
subject(:calculator) { described_class.new }
describe '#add' do
it 'adds two positive numbers' do
expect(calculator.add(2, 3)).to eq(5)
end
it 'handles negative numbers' do
expect(calculator.add(-1, 1)).to eq(0)
end
it 'handles zero' do
expect(calculator.add(0, 5)).to eq(5)
end
end
describe '#divide' do
context 'when divisor is not zero' do
it 'divides evenly' do
expect(calculator.divide(10, 2)).to eq(5)
end
it 'returns float for uneven division' do
expect(calculator.divide(10, 3)).to be_within(0.01).of(3.33)
end
end
context 'when divisor is zero' do
it 'raises ZeroDivisionError' do
expect { calculator.divide(10, 0) }.to raise_error(ZeroDivisionError)
end
end
end
end
Matchers Reference
# spec/matchers_examples_spec.rb
RSpec.describe 'RSpec Matchers' do
# Equality
it 'equality matchers' do
expect(5).to eq(5) # value equality (==)
expect(5).to eql(5) # type + value equality (eql?)
expect(obj).to equal(same_obj) # identity equality (equal?)
expect(obj).to be(same_obj) # alias for equal
end
# Comparison
it 'comparison matchers' do
expect(10).to be > 5
expect(10).to be >= 10
expect(10).to be < 20
expect(10).to be_between(5, 15).inclusive
expect(10).to be_between(5, 15).exclusive
expect(10.5).to be_within(0.1).of(10.4)
end
# Truthiness
it 'truthiness matchers' do
expect(true).to be_truthy
expect(false).to be_falsey
expect(nil).to be_nil
expect(1).to be_truthy # anything truthy
expect(nil).to be_falsey # nil is falsey
end
# Collections
it 'collection matchers' do
expect([1, 2, 3]).to include(2)
expect([1, 2, 3]).to contain_exactly(3, 1, 2) # order-independent
expect([1, 2, 3]).to match_array([3, 1, 2]) # alias
expect([1, 2, 3]).to start_with(1)
expect([1, 2, 3]).to end_with(3)
expect([]).to be_e
Установить RSpec Testing в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл RSpec Testing?
Comprehensive Ruby testing with RSpec including describe/context/it blocks, matchers, let/before hooks, mocking with doubles, shared examples, and Rails integration.
Как установить скилл RSpec Testing?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл RSpec Testing запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
от Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
от Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropic