Repository navigation
Cheat sheet
Cyril Kato edited this page Nov 1, 2026
·
1 revision
Every word and every matcher of RSpec clone, with what each line writes. The README says more about each. Each block is a file under spec/, run with ruby -r r_spec.
RSpec.describe Array do # a group; described_class is Array
describe "#size" do # a nested group; described_class is still Array
it { expect([].size).to be 0 }
end
context "when empty" do # a nested group, run in a child process
it { expect([]).to be_empty }
end
end
# spec/array_spec.rb:3: Success: expected 0 to be 0
# spec/array_spec.rb:7: Success: expected [] to be emptyRSpec.describe "helpers" do
let(:name) { "Bob" } # a method, called each time: no memoization
let(:twice) { |word| word * 2 }
subject { name.upcase } # let(:subject)
before { @count = 0 } # before each example, in the example
it { expect(name).to eq "Bob" }
it { expect(twice("ab")).to eq "abab" }
it { expect(subject).to eq "BOB" }
it { expect(@count).to be 0 }
end
# spec/helpers_spec.rb:7: Success: expected "Bob" to eq "Bob"
# spec/helpers_spec.rb:8: Success: expected "abab" to eq "abab"
# spec/helpers_spec.rb:9: Success: expected "BOB" to eq "BOB"
# spec/helpers_spec.rb:10: Success: expected 0 to be 0RSpec.describe Integer do
subject { 41 }
it("has a name") { expect(subject).to be 41 }
it { is_expected.to be 41 } # expect { subject }
its(:next) { is_expected.to be 42 } # expect { subject.next }
its(:+, 1) { is_expected.to be 42 } # expect { subject + 1 }
pending "is written later" # never run
end
# spec/integer_spec.rb:4: Success: expected 41 to be 41
# spec/integer_spec.rb:5: Success: expected 41 to be 41
# spec/integer_spec.rb:6: Success: expected 42 to be 42
# spec/integer_spec.rb:7: Success: expected 42 to be 42
# spec/integer_spec.rb:8: Pending: is written laterRSpec.it "judges a value or a block" do
expect(42).to be 42 # a value, already evaluated
expect { 42 }.to be 42 # a block, called by the matcher
expect { raise "boom" }.to raise_exception RuntimeError
expect(42).not_to be 43 # a negation
end
# spec/expectations_spec.rb:2: Success: expected 42 to be 42
# spec/expectations_spec.rb:3: Success: expected 42 to be 42
# spec/expectations_spec.rb:4: Success: expected #<RuntimeError: boom> to raise exception RuntimeError
# spec/expectations_spec.rb:5: Success: expected 42 not to be 43RSpec.it "has these matchers" do
expect("foo").to eq "foo" # expected.eql?(actual), same class
expect("foo").to eql "foo" # the same
expect(:foo).to be :foo # expected.equal?(actual)
expect(:foo).to equal :foo # the same
expect(true).to be_true
expect(false).to be_false
expect(nil).to be_nil
expect(42).to be_an_instance_of Integer
expect(42).to be_a_kind_of Comparable
expect(40).to be_within(2).of(42)
expect("bob@example.com").to match(/@/) # a Regexp only
expect { 1 / 0 }.to raise_exception ZeroDivisionError
expect { 1 / 0 }.to raise_exception ZeroDivisionError, "divided by 0"
expect { 1 / 0 }.to raise_exception ZeroDivisionError, /by 0/
expect([]).to be_empty # [].empty?
expect(foo: 1).to have_key :foo # { foo: 1 }.has_key?(:foo)
expect(42).to(satisfy { |value| value > 41 }) # the block answers true
end
# spec/matchers_spec.rb:2: Success: expected "foo" to eq "foo"
# spec/matchers_spec.rb:3: Success: expected "foo" to eq "foo"
# spec/matchers_spec.rb:4: Success: expected :foo to be :foo
# spec/matchers_spec.rb:5: Success: expected :foo to be :foo
# spec/matchers_spec.rb:6: Success: expected true to be true
# spec/matchers_spec.rb:7: Success: expected false to be false
# spec/matchers_spec.rb:8: Success: expected nil to be nil
# spec/matchers_spec.rb:9: Success: expected 42 to be an instance of Integer
# spec/matchers_spec.rb:10: Success: expected 42 to be a kind of Comparable
# spec/matchers_spec.rb:11: Success: expected 40 to be within 2 of 42
# spec/matchers_spec.rb:12: Success: expected "bob@example.com" to match /@/
# spec/matchers_spec.rb:13: Success: expected #<ZeroDivisionError: divided by 0> to raise exception ZeroDivisionError
# spec/matchers_spec.rb:14: Success: expected #<ZeroDivisionError: divided by 0> to raise exception ZeroDivisionError with message "divided by 0"
# spec/matchers_spec.rb:15: Success: expected #<ZeroDivisionError: divided by 0> to raise exception ZeroDivisionError with message /by 0/
# spec/matchers_spec.rb:16: Success: expected [] to be empty
# spec/matchers_spec.rb:17: Success: expected {foo: 1} to have key :foo
# spec/matchers_spec.rb:18: Success: expected 42 to satisfy &blocklist = []
RSpec.it "observes a change" do
expect { list << 1 }.to change(list, :length).by(1)
expect { list << 1 }.to change(list, :length).by_at_least(1)
expect { list << 1 }.to change(list, :length).by_at_most(1)
expect { list.clear }.to change(list, :length).from(3).to(0)
expect { list << 1 }.to change(list, :length).to(1)
end
# spec/change_spec.rb:4: Success: expected [1] to change by 1
# spec/change_spec.rb:5: Success: expected [1, 1] to change by at least 1
# spec/change_spec.rb:6: Success: expected [1, 1, 1] to change by at most 1
# spec/change_spec.rb:7: Success: expected [] to change from 3 to 0
# spec/change_spec.rb:8: Success: expected [1] to change to 1