rspec-skill について
rspec-skillは、RSpecでRubyのテストを書くためのリファレンススキルです。マッチャー、フック、遅延評価の`let`、ダブル、共有例、よくあるアンチパターンを解説します。
主な機能
- 01RSpecのテスト構成
- 02マッチャーと比較
- 03フックと遅延評価の`let`
- 04モック、ダブル、スタブ
- 05再利用可能な共有例
- 06アンチパターンの指針
活用シーン
- 01Rubyのクラスやサービスのテスト
- 02リポジトリ依存のスタブ化
- 03モデルテスト例の再利用
- 04テスト設定と可読性の改善
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill rspec-skillインストール後、Claude Code で/rspec-skillを入力して読み込まれたか確認します。
同梱ファイルreference/advanced-patterns.mdreference/playbook.md
詳細
RSpec テストスキル
基本パターン
基本的なテスト
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
end
describe '#divide' do
it 'divides evenly' do
expect(calculator.divide(10, 2)).to eq(5)
end
it 'raises on zero divisor' do
expect { calculator.divide(10, 0) }.to raise_error(ZeroDivisionError)
end
end
end
マッチャー
# Equality
expect(actual).to eq(expected) # ==
expect(actual).to eql(expected) # eql?
expect(actual).to equal(expected) # equal? (same object)
expect(actual).to be(expected) # equal?
# Comparison
expect(value).to be > 5
expect(value).to be_between(1, 10).inclusive
# Truthiness
expect(value).to be_truthy
expect(value).to be_falsey
expect(value).to be_nil
# Collections
expect(array).to include(3)
expect(array).to contain_exactly(1, 2, 3)
expect(array).to match_array([3, 1, 2])
expect(hash).to include(name: 'Alice')
# Strings
expect(str).to include('hello')
expect(str).to start_with('He')
expect(str).to match(/\d+/)
# Types
expect(obj).to be_a(String)
expect(obj).to be_an_instance_of(MyClass)
# Exceptions
expect { method }.to raise_error(StandardError)
expect { method }.to raise_error(StandardError, /message/)
# Change
expect { user.activate }.to change(user, :active).from(false).to(true)
expect { list.push(1) }.to change(list, :size).by(1)
フックと Let
RSpec.describe UserService do
let(:repo) { instance_double(UserRepository) }
let(:service) { described_class.new(repo) }
before(:each) do
allow(repo).to receive(:save).and_return(true)
end
after(:each) { cleanup }
before(:all) { setup_database }
after(:all) { teardown_database }
context 'when creating a user' do
it 'saves to repository' do
service.create('Alice', '[email protected]')
expect(repo).to have_received(:save).once
end
end
end
モックとスタブ
# Doubles
user = double('User', name: 'Alice', email: '[email protected]')
user = instance_double(User, name: 'Alice')
# Stubs
allow(service).to receive(:fetch).and_return(data)
allow(service).to receive(:fetch).with('id').and_return(user)
# Expectations
expect(service).to receive(:save).once
expect(service).to receive(:notify).with('[email protected]')
expect(service).not_to receive(:delete)
共有サンプル
RSpec.shared_examples 'a valid model' do
it { is_expected.to be_valid }
it { is_expected.to respond_to(:save) }
end
RSpec.describe User do
subject { described_class.new(name: 'Alice') }
it_behaves_like 'a valid model'
end
アンチパターン
| 悪い例 | 良い例 | 理由 |
|---|---|---|
重いセットアップを伴う before | let(遅延評価) | 使用されたときのみ評価されます |
| コンテキストがない | context 'when...' | シナリオが明確になります |
| インスタンス変数 | let ブロック | より簡潔で、遅延評価されます |
should 構文 | expect().to | 最新の RSpec の構文です |
セットアップ:gem install rspec の後に rspec --init を実行します
実行:bundle exec rspec または rspec spec/models/user_spec.rb
設定:--format documentation --color を指定した .rspec ファイル
発展的なパターン
高度なパターン、デバッグガイド、CI/CD 連携、ベストプラクティスについては、
reference/playbook.md を参照してください。
コメント
Markdown 対応