You are on page 1of 2

RSpec Matchers Cheatsheet

Basic Matchers

Other Common Matchers

> eq

> include

eq is the most commonly-used matcher and asserts equality.

include asserts that a value is present in the subject.

describe Array do
it 'has two things' do

describe 'dynamics of include' do


it 'works with arrays' do

expect(%w(thing1 thing2).length).to eq 2
end

expect([1, 2, 3]).to include 1


end

end
it "works with strings" do

> match
match asserts with a regular expression match.
describe User
User, '.build_with_default_ssn' do
it 'generates a valid SSN' do
user_with_default_ssn = User
User.build_with_default_ssn

expect('string').to include 'str'


end
it "works with hashes" do
expect({ foo: :bar }).to include :foo
end
end

expect(user_with_default_ssn.ssn).to match /^\d{3}-\d{2}-\d{4}$/


end
end

> be_true
be_true asserts truthiness.
describe User
User, '#admin?' do
it 'returns true when privileges include :admin' do
user = User
User.new(privileges: [:admin])
expect(user.admin?).to be_true
end
end

> be_false
be_false asserts falsiness.
describe User
User, '#admin?' do
it 'returns false when privileges are empty' do
user = User
User.new(privileges: [])
expect(user.admin?).to be_false
end
end

Copyright 2013 - thoughtbot, inc.

1 of 2

RSpec Matchers Cheatsheet

Exceptions

Predicates

> raise_error

RSpec is kind enough to match predicate methods (methods ending with a ?) with
appropriate matchers which read easily.

In exceptional situations, youll want to raise in Ruby. Testing can be done with expect,
passing a block, and then calling raise_error. The class and error message are
optional.
describe SocialNetworkNotifier
SocialNetworkNotifier, 'when the network is down' do
it 'raises when contacting Twitter fails' do
fake_network_outage

describe 'predicates' do
it 'works with all Ruby objects' do
expect(nil).to be_nil
expect(%w(thing1 thing2)).not_to be_empty
end
end

Predicates work on any Ruby object and any predicate method.

expect {
SocialNetworkNotifier
SocialNetworkNotifier.notify_twitter_with(MessageBuilder
MessageBuilder.generate)
}.to raise_error(SocialNetworkNotifier
SocialNetworkNotifier::NetworkUnreachableError
NetworkUnreachableError,

describe 'more predicates' do


it 'works with all Ruby objects' do

'Unable to contact Twitter.')

expect(User
User.new(privileges: [:admin])).to be_admin

end

expect(User
User.new(privileges: [])).not_to be_admin
end

it 'raises when contacting Facebook fails' do

end

fake_network_outage
expect {
SocialNetworkNotifier
SocialNetworkNotifier.notify_facebook_with(MessageBuilder
MessageBuilder.generate)
}.to raise_error(SocialNetworkNotifier
SocialNetworkNotifier::NetworkUnreachableError
NetworkUnreachableError, /Facebook/)
end
end

raise_error also works with not_to.


describe SocialNetworkNotifier
SocialNetworkNotifier, 'when the network is working' do
it 'does not raise when contacting Twitter' do
fake_network_success
expect {
SocialNetworkNotifier
SocialNetworkNotifier.notify_twitter_with(MessageBuilder
MessageBuilder.generate)
}.not_to raise_error
end
end

Copyright 2013 - thoughtbot, inc.

2 of 2

You might also like