rspec-skill
Generates 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".
What this skill does
# RSpec Testing Skill
## Core Patterns
### Basic Test
```ruby
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
```
### Matchers
```ruby
# 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)
```
### Hooks and Let
```ruby
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
```
### Mocking and Stubbing
```ruby
# 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)
```
### Shared Examples
```ruby
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
```
### Anti-Patterns
| Bad | Good | Why |
|-----|------|-----|
| `before` with heavy setup | `let` (lazy) | Only evaluates when used |
| No contexts | `context 'when...'` | Clear scenarios |
| Instance variables | `let` blocks | Cleaner, lazier |
| `should` syntax | `expect().to` | Modern RSpec |
## Setup: `gem install rspec` then `rspec --init`
## Run: `bundle exec rspec` or `rspec spec/models/user_spec.rb`
## Config: `.rspec` file with `--format documentation --color`
## Deep Patterns
For advanced patterns, debugging guides, CI/CD integration, and best practices,
see `reference/playbook.md`.
Related in unit-testing
phpunit-skill
IncludedGenerates PHPUnit tests in PHP. Covers assertions, data providers, mocking, and test doubles. Use when user mentions "PHPUnit", "TestCase", "assertEquals", "PHP test". Triggers on: "PHPUnit", "TestCase PHP", "assertEquals PHP", "PHP unit test".
karma-skill
IncludedGenerates Karma test runner configurations for browser-based JavaScript testing. Works with Jasmine, Mocha, or QUnit. Use when user mentions "Karma", "karma.conf.js", "browser test runner". Triggers on: "Karma", "karma.conf", "karma test runner", "browser-based JS test".
unittest-skill
IncludedGenerates Python unittest tests. Built-in testing framework with TestCase, setUp/tearDown, and assertion methods. Use when user mentions "unittest", "TestCase", "self.assertEqual", "Python unittest". Triggers on: "unittest", "TestCase", "self.assertEqual", "Python unittest" (not pytest).
testng-skill
IncludedGenerates TestNG tests in Java with groups, data providers, parallel execution, XML suite configuration, and listeners. Use when user mentions "TestNG", "@DataProvider", "testng.xml", "groups". Triggers on: "TestNG", "@DataProvider", "testng.xml", "TestNG suite", "parallel tests Java".
jest-skill
IncludedGenerates Jest unit and integration tests in JavaScript or TypeScript. Covers mocking, snapshots, async testing, and React component testing. Use when user mentions "Jest", "describe/it/expect", "jest.mock", "toMatchSnapshot". Triggers on: "Jest", "expect().toBe()", "jest.mock", "snapshot test", "JS test", "React test".
junit-5-skill
IncludedGenerates production-grade JUnit 5 unit and integration tests in Java. Covers assertions, parameterized tests, lifecycle hooks, mocking with Mockito, and nested tests. Use when user mentions "JUnit", "JUnit 5", "@Test", "assertEquals", "Assertions", "Java unit test". Triggers on: "JUnit", "@Test", "assertEquals", "Java test", "unit test Java".