RSpec:Expectations Cheat Sheet
RSpec:Expectations Cheat Sheet
RSpec:Expectations Cheat Sheet
Aug 9, 2016
Continuing the from the previous post, here is a cheat sheet for RSpecs
assertion library: RSpec::Expectations.
RSpec is a mature, feature-packed testing framework, but the
documentation can be difficult to navigate. As an alternative to the official
documentation, this cheat sheet contains short example code to
demonstrate all the built-in expectation matchers.
These examples were created with RSpec 3.5. All of the examples are
passing there are no examples of failing tests. They cover the
functionality in RSpec::Expectations functionality for expressing
assertions inside tests using expect. For examples of RSpec.describe, it,
let, etc., see the previous post: RSpec::Core Cheat Sheet.
Basic Matchers
These are the most commonly used matchers.
RSpec.describe 'Common, built-in expectation matchers' do
example 'Equality' do
expect('x'+'y').to
eq('xy')
# a == b
expect('x'+'y').to
eql('xy') # a.eql?(b)
expect('x'+'y').not_to be('xy')
# a.equal?(b)
end
example 'Strings' do
expect('abcd').to include('bc')
expect('abcd').to start_with 'ab'
expect('abcd').to end_with 'cd'
expect('abcd').to match /[a-z]+/
end
example 'Collections' do
expect([1, 2, 3]).to include(1, 3)
expect([1, 2, 3]).to contain_exactly(3, 2, 1) # order not important
expect({ a: 1, b: 2 }).to include(b: 2)
end
example 'Booleans and nil' do
expect(true).to be true
expect(false).to be false
expect('abc').to be_truthy
expect(nil).to be_falsey
expect(nil).to be_nil
end
example 'Numeric' do
expect(5).to be > 4
expect(5).to be >= 4
expect(5).to be < 6
expect(5).to be <= 6
expect(5).to be_between(4, 6).exclusive
expect(5).to be_between(5, 6).inclusive
expect(4.99).to be_within(0.02).of(5)
end
example 'Errors
expect{ 5 / 0
expect{ 5 / 0
expect{ 5 / 0
end
end
(exceptions)' do
}.to raise_error(ZeroDivisionError)
}.to raise_error("divided by 0")
}.to raise_error(ZeroDivisionError, "divided by 0")
Predicate Matchers
Predicate matchers are a little DSL for calling predicate methods.
Predicate methods are methods that:
1. return a boolean value; and
2. have a name that ends with ?
Commonly used predicate methods in the Ruby standard library include:
Object#nil?, Array#empty?, and Hash#has_key?.
RSpec.describe 'Predicate matchers' do
example 'Array' do
expect([]).to be_empty
# [].empty?
end
example 'Hash' do
expect({a: 1}).to have_key(:a)
expect({a: 1}).to have_value(1)
end
# {a: 1}.has_key?(:a)
# {a: 1}.has_value?(1)
example 'Object' do
expect(5).not_to be_nil
expect(5).to be_instance_of Fixnum
expect(5).to be_kind_of Numeric
end
end
# 'hi'.nil?
# 5.instance_of?(Fixnum)
# 5.kind_of?(Numeric)
have_cliche_name
be_hacker
be_a_hacker
be_an_hacker
There are a few more predicate matchers, but they are rarely used.
Advanced Matchers
These are the more complicated matchers, that arent as commonly used.
# Add one extra method to the Widget class above,
# for demonstrating change observation.
class Widget
def fifty_percent_off!
@cost /= 2
end
end
RSpec.describe 'Advanced matchers' do
example 'Change observation' do
widget = Widget.new('Baz', 80)
expect{ widget.has_cliche_name? }.not_to change(widget, :name)