Logo: Relish

  1. Sign up
  2. Sign in

Project: RSpec Expectations 3.0

Built in matchers

Built-in Matchers

rspec-expectations ships with a number of built-in matchers.
Each matcher can be used with expect(..).to or expect(..).not_to to define
positive and negative expectations respectively on an object. Most matchers can
also be accessed using the (...).should and (...).should_not syntax, see
using should syntax
for why we recommend using expect.

e.g.

expect(result).to   eq(3)
expect(list).not_to be_empty
pi.should be > 3

Object identity

expect(actual).to be(expected) # passes if actual.equal?(expected)

Object equivalence

expect(actual).to eq(expected) # passes if actual == expected

Optional APIs for identity/equivalence

expect(actual).to eql(expected)   # passes if actual.eql?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)

# NOTE: `expect` does not support `==` matcher.

Comparisons

expect(actual).to be >  expected
expect(actual).to be >= expected
expect(actual).to be <= expected
expect(actual).to be <  expected
expect(actual).to match(/expression/)
expect(actual).to be_within(delta).of(expected)

# NOTE: `expect` does not support `=~` matcher.

Types/classes

expect(actual).to be_instance_of(expected)
expect(actual).to be_kind_of(expected)

Truthiness and existentialism

expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true   # passes if actual == true
expect(actual).to be_falsey # passes if actual is falsy (nil or false)
expect(actual).to be false  # passes if actual == false
expect(actual).to be_nil    # passes if actual is nil

Expecting errors

expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")

Expecting throws

expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')

Predicate matchers

expect(actual).to be_xxx         # passes if actual.xxx?
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)

Examples

expect([]).to      be_empty
expect(:a => 1).to have_key(:a)

Collection membership

expect(actual).to include(expected)
expect(array).to match_array(expected_array)
# ...which is the same as:
expect(array).to contain_exactly(individual, elements)

Examples

expect([1, 2, 3]).to     include(1)
expect([1, 2, 3]).to     include(1, 2)
expect(:a => 'b').to     include(:a => 'b')
expect("this string").to include("is str")
expect([1, 2, 3]).to     contain_exactly(2, 1, 3)
expect([1, 2, 3]).to     match_array([3, 2, 1])

Ranges (1.9 only)

expect(1..10).to cover(3)

Topics

  1. "be" matchers
  2. be_within matcher
  3. equality matchers
  4. exist matcher
  5. change matcher
  6. raise_error matcher
  7. include matcher
  8. match matcher
  9. predicate matchers
  10. respond_to matcher
  11. satisfy matcher
  12. throw_symbol matcher
  13. specify types of objects
  14. yield matchers
  15. comparison matchers
  16. contain_exactly matcher
  17. cover matcher
  18. end_with matcher
  19. output matcher
  20. start_with matcher

Last published about 1 month ago by myronmarston.