Module RSpec::Matchers
In: lib/rspec/matchers.rb
lib/rspec/matchers/be.rb
lib/rspec/matchers/be_close.rb
lib/rspec/matchers/be_instance_of.rb
lib/rspec/matchers/be_kind_of.rb
lib/rspec/matchers/be_within.rb
lib/rspec/matchers/block_aliases.rb
lib/rspec/matchers/change.rb
lib/rspec/matchers/cover.rb
lib/rspec/matchers/dsl.rb
lib/rspec/matchers/eq.rb
lib/rspec/matchers/eql.rb
lib/rspec/matchers/equal.rb
lib/rspec/matchers/errors.rb
lib/rspec/matchers/exist.rb
lib/rspec/matchers/extensions/instance_eval_with_args.rb
lib/rspec/matchers/generated_descriptions.rb
lib/rspec/matchers/has.rb
lib/rspec/matchers/have.rb
lib/rspec/matchers/include.rb
lib/rspec/matchers/match.rb
lib/rspec/matchers/match_array.rb
lib/rspec/matchers/matcher.rb
lib/rspec/matchers/method_missing.rb
lib/rspec/matchers/operator_matcher.rb
lib/rspec/matchers/pretty.rb
lib/rspec/matchers/raise_error.rb
lib/rspec/matchers/respond_to.rb
lib/rspec/matchers/satisfy.rb
lib/rspec/matchers/throw_symbol.rb

rspec-expecations provides a number of useful Matchers we use to compose expectations. A Matcher is any object that responds to the following:

 matches?(actual)
 failure_message_for_should

These methods are also part of the matcher protocol, but are optional:

 does_not_match?(actual)
 failure_message_for_should_not
 description #optional

Predicates

In addition to those Expression Matchers that are defined explicitly, RSpec will create custom Matchers on the fly for any arbitrary predicate, giving your specs a much more natural language feel.

A Ruby predicate is a method that ends with a "?" and returns true or false. Common examples are +empty?+, +nil?+, and +instance_of?+.

All you need to do is write +should be_+ followed by the predicate without the question mark, and RSpec will figure it out from there. For example:

 [].should be_empty     # => [].empty?() | passes
 [].should_not be_empty # => [].empty?() | fails

In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_" and "be_an_", making your specs read much more naturally:

 "a string".should be_an_instance_of(String) =>"a string".instance_of?(String) #passes

 3.should be_a_kind_of(Fixnum)        # => 3.kind_of?(Numeric)     | passes
 3.should be_a_kind_of(Numeric)       # => 3.kind_of?(Numeric)     | passes
 3.should be_an_instance_of(Fixnum)   # => 3.instance_of?(Fixnum)  | passes
 3.should_not be_instance_of(Numeric) # => 3.instance_of?(Numeric) | fails

RSpec will also create custom matchers for predicates like +has_key?+. To use this feature, just state that the object should have_key(:key) and RSpec will call has_key?(:key) on the target. For example:

 {:a => "A"}.should have_key(:a) # => {:a => "A"}.has_key?(:a) | passes
 {:a => "A"}.should have_key(:b) # => {:a => "A"}.has_key?(:b) | fails

You can use this feature to invoke any predicate that begins with "has_", whether it is part of the Ruby libraries (like +Hash#has_key?+) or a method you wrote on your own class.

Custom Matchers

When you find that none of the stock Expectation Matchers provide a natural feeling expectation, you can very easily write your own using RSpec‘s matcher DSL or writing one from scratch.

Matcher DSL

Imagine that you are writing a game in which players can be in various zones on a virtual board. To specify that bob should be in zone 4, you could say:

 bob.current_zone.should eql(Zone.new("4"))

But you might find it more expressive to say:

 bob.should be_in_zone("4")

and/or

 bob.should_not be_in_zone("3")

You can create such a matcher like so:

 RSpec::Matchers.define :be_in_zone do |zone|
   match do |player|
     player.in_zone?(zone)
   end
 end

This will generate a be_in_zone method that returns a matcher with logical default messages for failures. You can override the failure messages and the generated description as follows:

 RSpec::Matchers.define :be_in_zone do |zone|
   match do |player|
     player.in_zone?(zone)
   end

   failure_message_for_should do |player|
     # generate and return the appropriate string.
   end

   failure_message_for_should_not do |player|
     # generate and return the appropriate string.
   end

   description do
     # generate and return the appropriate string.
   end
 end

Each of the message-generation methods has access to the block arguments passed to the create method (in this case, zone). The failure message methods (failure_message_for_should and failure_message_for_should_not) are passed the actual value (the receiver of should or should_not).

Custom Matcher from scratch

You could also write a custom matcher from scratch, as follows:

 class BeInZone
   def initialize(expected)
     @expected = expected
   end

   def matches?(target)
     @target = target
     @target.current_zone.eql?(Zone.new(@expected))
   end

   def failure_message_for_should
     "expected #{@target.inspect} to be in Zone #{@expected}"
   end

   def failure_message_for_should_not
     "expected #{@target.inspect} not to be in Zone #{@expected}"
   end
 end

… and a method like this:

 def be_in_zone(expected)
   BeInZone.new(expected)
 end

And then expose the method to your specs. This is normally done by including the method and the class in a module, which is then included in your spec:

 module CustomGameMatchers
   class BeInZone
     # ...
   end

   def be_in_zone(expected)
     # ...
   end
 end

 describe "Player behaviour" do
   include CustomGameMatchers
   # ...
 end

or you can include in globally in a spec_helper.rb file required from your spec file(s):

 RSpec::configure do |config|
   config.include(CustomGameMatchers)
 end

Methods

Classes and Modules

Module RSpec::Matchers::BlockAliases
Module RSpec::Matchers::DSL
Module RSpec::Matchers::Extensions
Module RSpec::Matchers::Pretty
Class RSpec::Matchers::Be
Class RSpec::Matchers::BeComparedTo
Class RSpec::Matchers::BePredicate
Class RSpec::Matchers::Change
Class RSpec::Matchers::Has
Class RSpec::Matchers::Have
Class RSpec::Matchers::MatchArray
Class RSpec::Matchers::Matcher
Class RSpec::Matchers::MatcherError
Class RSpec::Matchers::NegativeOperatorMatcher
Class RSpec::Matchers::OperatorMatcher
Class RSpec::Matchers::PositiveOperatorMatcher
Class RSpec::Matchers::RaiseError
Class RSpec::Matchers::RespondTo
Class RSpec::Matchers::Satisfy
Class RSpec::Matchers::ThrowSymbol

Attributes

last_matcher  [RW] 
last_should  [RW] 

Public Class methods

Public Instance methods

@example

  actual.should be_true
  actual.should be_false
  actual.should be_nil
  actual.should be_[arbitrary_predicate](*args)
  actual.should_not be_nil
  actual.should_not be_[arbitrary_predicate](*args)

Given true, false, or nil, will pass if actual value is true, false or nil (respectively). Given no args means the caller should satisfy an if condition (to be or not to be).

Predicates are any Ruby method that ends in a "?" and returns true or false. Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match convert that into a query against the target object.

The arbitrary_predicate feature will handle any predicate prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of) or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.

passes if target.kind_of?(klass)

Passes if actual.kind_of?(expected)

Examples

  5.should be_kind_of(Fixnum)
  5.should be_kind_of(Numeric)
  5.should_not be_kind_of(Float)
be_an(klass)

Alias for be_a

Passes if actual.instance_of?(expected)

Examples

  5.should be_instance_of(Fixnum)
  5.should_not be_instance_of(Numeric)
  5.should_not be_instance_of(Float)

@deprecated use be_within instead.

be_instance_of(expected)

Alias for be_an_instance_of

be_kind_of(expected)

Alias for be_a_kind_of

Passes if actual == expected +/- delta

Examples

  result.should be_within(0.5).of(3.0)
  result.should_not be_within(0.5).of(3.0)

Applied to a proc, specifies that its execution will cause some value to change.

@param [Object] receiver @param [Symbol] message the message to send the receiver

You can either pass receiver and message, or a block, but not both.

When passing a block, it must use the { … } format, not do/end, as { … } binds to the change method, whereas do/end would errantly bind to the should or should_not method.

Examples

  lambda {
    team.add_player(player)
  }.should change(roster, :count)

  lambda {
    team.add_player(player)
  }.should change(roster, :count).by(1)

  lambda {
    team.add_player(player)
  }.should change(roster, :count).by_at_least(1)

  lambda {
    team.add_player(player)
  }.should change(roster, :count).by_at_most(1)

  string = "string"
  lambda {
    string.reverse!
  }.should change { string }.from("string").to("gnirts")

  lambda {
    person.happy_birthday
  }.should change(person, :birthday).from(32).to(33)

  lambda {
    employee.develop_great_new_social_networking_app
  }.should change(employee, :title).from("Mail Clerk").to("CEO")

  lambda {
    doctor.leave_office
  }.should change(doctor, :sign).from(/is in/).to(/is out/)

  user = User.new(:type => "admin")
  lambda {
    user.symbolize_type
  }.should change(user, :type).from(String).to(Symbol)

Notes

Evaluates receiver.message or block before and after it evaluates the proc object (generated by the lambdas in the examples above).

should_not change only supports the form with no subsequent calls to by, by_at_least, by_at_most, to or from.

Passes if actual covers expected. This works for Ranges. You can also pass in multiple args and it will only pass if all args are found in Range.

Examples

  (1..10).should cover(5)
  (1..10).should cover(4, 6)
  (1..10).should cover(4, 6, 11) # will fail
  (1..10).should_not cover(11)
  (1..10).should_not cover(5)    # will fail

Warning: Ruby >= 1.9 only

Passes if actual == expected.

See www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.

Examples

  5.should eq(5)
  5.should_not eq(3)

Passes if +actual.eql?(expected)+

See www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.

Examples

  5.should eql(5)
  5.should_not eql(3)

Passes if actual.equal?(expected) (object identity).

See www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.

Examples

  5.should equal(5) # Fixnums are equal
  "5".should_not equal("5") # Strings that look the same are not the same object

Passes if actual.exist? or actual.exists?

Extends the submitted block with aliases to and to_not for should and should_not. Allows expectations like this:

  expect { this_block }.to change{this.expression}.from(old_value).to(new_value)
  expect { this_block }.to raise_error

Passes if receiver is a collection with the submitted number of items OR if the receiver OWNS a collection with the submitted number of items.

If the receiver OWNS the collection, you must use the name of the collection. So if a Team instance has a collection named players, you must use that name to set the expectation.

If the receiver IS the collection, you can use any name you like for named_collection. We‘d recommend using either "elements", "members", or "items" as these are all standard ways of describing the things IN a collection.

This also works for Strings, letting you set an expectation about its length

Examples

  # Passes if team.players.size == 11
  team.should have(11).players

  # Passes if [1,2,3].length == 3
  [1,2,3].should have(3).items #"items" is pure sugar

  # Passes if ['a', 'b', 'c'].count == 3
  [1,2,3].should have(3).items #"items" is pure sugar

  # Passes if "this string".length == 11
  "this string".should have(11).characters #"characters" is pure sugar

Exactly like have() with >=.

Warning

should_not have_at_least is not supported

Exactly like have() with <=.

Warning

should_not have_at_most is not supported

have_exactly(n)

Alias for have

Passes if actual includes expected. This works for collections and Strings. You can also pass in multiple args and it will only pass if all args are found in collection.

Examples

  [1,2,3].should include(3)
  [1,2,3].should include(2,3) #would pass
  [1,2,3].should include(2,3,4) #would fail
  [1,2,3].should_not include(4)
  "spread".should include("read")
  "spread".should_not include("red")

Given a Regexp or String, passes if actual.match(pattern)

Examples

  email.should match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
  email.should match("@example.com")

With no args, matches if any error is raised. With a named error, matches only if that specific error is raised. With a named error and messsage specified as a String, matches only if both match. With a named error and messsage specified as a Regexp, matches only if both match. Pass an optional block to perform extra verifications on the exception matched

Examples

  lambda { do_something_risky }.should raise_error
  lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
  lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) { |error| error.data.should == 42 }
  lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky")
  lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/)

  lambda { do_something_risky }.should_not raise_error
  lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError)
  lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky")
  lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/)
raise_exception(error=Exception, message=nil, &block)

Alias for raise_error

Matches if the target object responds to all of the names provided. Names can be Strings or Symbols.

Examples

Passes if the submitted block returns true. Yields target to the block.

Generally speaking, this should be thought of as a last resort when you can‘t find any other way to specify the behaviour you wish to specify.

If you do find yourself in such a situation, you could always write a custom matcher, which would likely make your specs more expressive.

Examples

  5.should satisfy { |n|
    n > 3
  }

Given no argument, matches if a proc throws any Symbol.

Given a Symbol, matches if the given proc throws the specified Symbol.

Given a Symbol and an arg, matches if the given proc throws the specified Symbol with the specified arg.

Examples

  lambda { do_something_risky }.should throw_symbol
  lambda { do_something_risky }.should throw_symbol(:that_was_risky)
  lambda { do_something_risky }.should throw_symbol(:that_was_risky, culprit)

  lambda { do_something_risky }.should_not throw_symbol
  lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
  lambda { do_something_risky }.should_not throw_symbol(:that_was_risky, culprit)

[Validate]