$npx -y skills add LambdaTest/agent-skills --skill rspec-skillGenerates RSpec tests in Ruby with describe/context/it blocks, matchers, let/before hooks, and mocking. Use when user mentions "RSpec", "describe do", "expect().to", "Ruby test". Triggers on: "RSpec", "expect().to eq()", "describe do", "Ruby test", "spec file".
| 1 | # RSpec Testing Skill |
| 2 | |
| 3 | ## Core Patterns |
| 4 | |
| 5 | ### Basic Test |
| 6 | |
| 7 | ```ruby |
| 8 | RSpec.describe Calculator do |
| 9 | subject(:calculator) { described_class.new } |
| 10 | |
| 11 | describe '#add' do |
| 12 | it 'adds two positive numbers' do |
| 13 | expect(calculator.add(2, 3)).to eq(5) |
| 14 | end |
| 15 | |
| 16 | it 'handles negative numbers' do |
| 17 | expect(calculator.add(-1, 1)).to eq(0) |
| 18 | end |
| 19 | end |
| 20 | |
| 21 | describe '#divide' do |
| 22 | it 'divides evenly' do |
| 23 | expect(calculator.divide(10, 2)).to eq(5) |
| 24 | end |
| 25 | |
| 26 | it 'raises on zero divisor' do |
| 27 | expect { calculator.divide(10, 0) }.to raise_error(ZeroDivisionError) |
| 28 | end |
| 29 | end |
| 30 | end |
| 31 | ``` |
| 32 | |
| 33 | ### Matchers |
| 34 | |
| 35 | ```ruby |
| 36 | # Equality |
| 37 | expect(actual).to eq(expected) # == |
| 38 | expect(actual).to eql(expected) # eql? |
| 39 | expect(actual).to equal(expected) # equal? (same object) |
| 40 | expect(actual).to be(expected) # equal? |
| 41 | |
| 42 | # Comparison |
| 43 | expect(value).to be > 5 |
| 44 | expect(value).to be_between(1, 10).inclusive |
| 45 | |
| 46 | # Truthiness |
| 47 | expect(value).to be_truthy |
| 48 | expect(value).to be_falsey |
| 49 | expect(value).to be_nil |
| 50 | |
| 51 | # Collections |
| 52 | expect(array).to include(3) |
| 53 | expect(array).to contain_exactly(1, 2, 3) |
| 54 | expect(array).to match_array([3, 1, 2]) |
| 55 | expect(hash).to include(name: 'Alice') |
| 56 | |
| 57 | # Strings |
| 58 | expect(str).to include('hello') |
| 59 | expect(str).to start_with('He') |
| 60 | expect(str).to match(/\d+/) |
| 61 | |
| 62 | # Types |
| 63 | expect(obj).to be_a(String) |
| 64 | expect(obj).to be_an_instance_of(MyClass) |
| 65 | |
| 66 | # Exceptions |
| 67 | expect { method }.to raise_error(StandardError) |
| 68 | expect { method }.to raise_error(StandardError, /message/) |
| 69 | |
| 70 | # Change |
| 71 | expect { user.activate }.to change(user, :active).from(false).to(true) |
| 72 | expect { list.push(1) }.to change(list, :size).by(1) |
| 73 | ``` |
| 74 | |
| 75 | ### Hooks and Let |
| 76 | |
| 77 | ```ruby |
| 78 | RSpec.describe UserService do |
| 79 | let(:repo) { instance_double(UserRepository) } |
| 80 | let(:service) { described_class.new(repo) } |
| 81 | |
| 82 | before(:each) do |
| 83 | allow(repo).to receive(:save).and_return(true) |
| 84 | end |
| 85 | |
| 86 | after(:each) { cleanup } |
| 87 | before(:all) { setup_database } |
| 88 | after(:all) { teardown_database } |
| 89 | |
| 90 | context 'when creating a user' do |
| 91 | it 'saves to repository' do |
| 92 | service.create('Alice', 'alice@test.com') |
| 93 | expect(repo).to have_received(:save).once |
| 94 | end |
| 95 | end |
| 96 | end |
| 97 | ``` |
| 98 | |
| 99 | ### Mocking and Stubbing |
| 100 | |
| 101 | ```ruby |
| 102 | # Doubles |
| 103 | user = double('User', name: 'Alice', email: 'alice@test.com') |
| 104 | user = instance_double(User, name: 'Alice') |
| 105 | |
| 106 | # Stubs |
| 107 | allow(service).to receive(:fetch).and_return(data) |
| 108 | allow(service).to receive(:fetch).with('id').and_return(user) |
| 109 | |
| 110 | # Expectations |
| 111 | expect(service).to receive(:save).once |
| 112 | expect(service).to receive(:notify).with('alice@test.com') |
| 113 | expect(service).not_to receive(:delete) |
| 114 | ``` |
| 115 | |
| 116 | ### Shared Examples |
| 117 | |
| 118 | ```ruby |
| 119 | RSpec.shared_examples 'a valid model' do |
| 120 | it { is_expected.to be_valid } |
| 121 | it { is_expected.to respond_to(:save) } |
| 122 | end |
| 123 | |
| 124 | RSpec.describe User do |
| 125 | subject { described_class.new(name: 'Alice') } |
| 126 | it_behaves_like 'a valid model' |
| 127 | end |
| 128 | ``` |
| 129 | |
| 130 | ### Anti-Patterns |
| 131 | |
| 132 | | Bad | Good | Why | |
| 133 | |-----|------|-----| |
| 134 | | `before` with heavy setup | `let` (lazy) | Only evaluates when used | |
| 135 | | No contexts | `context 'when...'` | Clear scenarios | |
| 136 | | Instance variables | `let` blocks | Cleaner, lazier | |
| 137 | | `should` syntax | `expect().to` | Modern RSpec | |
| 138 | |
| 139 | ## Setup: `gem install rspec` then `rspec --init` |
| 140 | ## Run: `bundle exec rspec` or `rspec spec/models/user_spec.rb` |
| 141 | ## Config: `.rspec` file with `--format documentation --color` |
| 142 | |
| 143 | ## Deep Patterns |
| 144 | |
| 145 | For advanced patterns, debugging guides, CI/CD integration, and best practices, |
| 146 | see `reference/playbook.md`. |