module RSpec::Matchers
def self.clear_generated_description
def self.clear_generated_description self.last_matcher = nil self.last_should = nil end
def self.configuration
-
(RSpec::Expectations::Configuration)- the configuration object
def self.configuration @configuration ||= Expectations::Configuration.new end
def self.const_missing(name)
def self.const_missing(name) case name when :OperatorMatcher RSpec.deprecate("`RSpec::Matchers::OperatorMatcher`", :replacement => "`RSpec::Matchers::BuiltIn::OperatorMatcher`") BuiltIn::OperatorMatcher when :Configuration RSpec.deprecate("`RSpec::Matchers::Configuration`", :replacement => "`RSpec::Expectations::Configuration`") Expectations::Configuration else super end end
def self.generated_description
def self.generated_description return nil if last_should.nil? "#{last_should.to_s.gsub('_',' ')} #{last_description}" end
def self.is_a_matcher?(obj)
- Api: - private
def self.is_a_matcher?(obj) return true if ::RSpec::Matchers::BuiltIn::BaseMatcher === obj return false if obj.respond_to?(:i_respond_to_everything_so_im_not_really_a_matcher) return false unless obj.respond_to?(:matches?) obj.respond_to?(:failure_message_for_should) || obj.respond_to?(:failure_message) end
def self.last_description
def self.last_description last_matcher.respond_to?(:description) ? last_matcher.description : <<-MESSAGE you call a matcher in an example without a String, like this: ify { object.should matcher } his: should matcher } c expects the matcher to have a #description method. You should either a String to the example this matcher is being used in, or give it a ription method. Then you won't have to suffer this lengthy warning again. AGE end
def be(*args)
(e.g. be_empty), letting you choose the prefix that best suits the
"be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of) or "be_"
The arbitrary_predicate feature will handle any predicate prefixed with
RSpec will match convert that into a query against the target object.
false. Given be_ followed by arbitrary_predicate (without the "?"),
Predicates are any Ruby method that ends in a "?" and returns true or
condition (to be or not to be).
nil (respectively). Given no args means the caller should satisfy an if
Given true, false, or nil, will pass if actual value is true, false or
expect(actual).not_to be_[arbitrary_predicate](*args)
expect(actual).not_to be_nil
expect(actual).to be_[arbitrary_predicate](*args)
expect(actual).to be_nil
expect(actual).to be_falsey
expect(actual).to be_truthy
@example
def be(*args) args.empty? ? Matchers::BuiltIn::Be.new : equal(*args) end
def be_a(klass)
def be_a(klass) be_a_kind_of(klass) end
def be_a_kind_of(expected)
expect(5).to be_a_kind_of(Numeric)
expect(5).to be_a_kind_of(Fixnum)
@example
Passes if actual.kind_of?(expected)
def be_a_kind_of(expected) BuiltIn::BeAKindOf.new(expected) end
def be_an_instance_of(expected)
expect(5).not_to be_an_instance_of(Numeric)
expect(5).to be_an_instance_of(Fixnum)
@example
Passes if actual.instance_of?(expected)
def be_an_instance_of(expected) BuiltIn::BeAnInstanceOf.new(expected) end
def be_close(expected, delta)
- use +be_within+ instead.
def be_close(expected, delta) RSpec.deprecate("be_close(#{expected}, #{delta})", :replacement => "be_within(#{delta}).of(#{expected})", :type => 'the be_close matcher' ) be_within(delta).of(expected) end
def be_false
def be_false RSpec.deprecate("`be_false`", :replacement => "`be_falsey` (for Ruby's conditional semantics) or " + "`be false` (for exact `== false` equality)" ) BuiltIn::BeFalsey.new end
def be_falsey
def be_falsey BuiltIn::BeFalsey.new end
def be_nil
def be_nil BuiltIn::BeNil.new end
def be_true
def be_true RSpec.deprecate("`be_true`", :replacement => "`be_truthy` (for Ruby's conditional semantics) or " + "`be true` (for exact `== true` equality)" ) BuiltIn::BeTruthy.new end
def be_truthy
def be_truthy BuiltIn::BeTruthy.new end
def be_within(delta)
expect(result).to be_within(0.5).of(3.0)
@example
Passes if actual == expected +/- delta
def be_within(delta) BuiltIn::BeWithin.new(delta) end
def change(receiver=nil, message=nil, &block)
-
message(Symbol) -- the message to send the receiver -
receiver(Object) --
def change(receiver=nil, message=nil, &block) BuiltIn::Change.new(receiver, message, &block) end
def cover(*values)
expect(1..10).not_to cover(5) # fails
expect(1..10).not_to cover(11)
expect(1..10).to cover(4, 6, 11) # fails
expect(1..10).to cover(4, 6)
expect(1..10).to cover(5)
@example
and it will only pass if all args are found in Range.
Ranges. You can also pass in multiple args
Passes if actual covers expected. This works for
def cover(*values) BuiltIn::Cover.new(*values) end if (1..2).respond_to?(:cover?)
def end_with(*expected)
expect([0, 1, 2, 3, 4]).to end_with 4
expect("this string").to end_with "string"
@example
`expected.length` elements of the actual array.
actual string. In the case of an array, matches against the last
of a string, matches against the last `expected.length` characters of the
Matches if the actual value ends with the expected value(s). In the case
def end_with(*expected) BuiltIn::EndWith.new(*expected) end
def eq(expected)
expect(5).to eq(5)
@example
information about equality in Ruby.
See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
Passes if actual == expected.
def eq(expected) BuiltIn::Eq.new(expected) end
def eql(expected)
expect(5).to eql(5)
@example
information about equality in Ruby.
See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
Passes if +actual.eql?(expected)+
def eql(expected) BuiltIn::Eql.new(expected) end
def equal(expected)
expect(5).to equal(5) # Fixnums are equal
@example
information about equality in Ruby.
See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
Passes if actual.equal?(expected) (object identity).
def equal(expected) BuiltIn::Equal.new(expected) end
def exist(*args)
@example
Passes if `actual.exist?` or `actual.exists?`
def exist(*args) BuiltIn::Exist.new(*args) end
def have(n)
# Passes if "this string".length == 11
expect([1,2,3]).to have(3).items #"items" is pure sugar
# Passes if ['a', 'b', 'c'].count == 3
expect([1,2,3]).to have(3).items #"items" is pure sugar
# Passes if [1,2,3].length == 3
expect(team).to have(11).players
# Passes if team.players.size == 11
@example
lengths.
This also works for Strings, letting you set expectations about their
collection.
"items" as these are all standard ways of describing the things IN a
`named_collection`. We'd recommend using either "elements", "members", or
If the receiver IS the collection, you can use any name you like for
you must use that name to set the expectation.
collection. So if a `Team` instance has a collection named `#players`,
If the receiver OWNS the collection, you must use the name of the
if the receiver OWNS a collection with the submitted number of items.
Passes if receiver is a collection with the submitted number of items OR
def have(n) BuiltIn::Have.new(n) end
def have_at_least(n)
### Warning:
expect("this").to have_at_least(3).letters
@example
Exactly like have() with >=.
def have_at_least(n) BuiltIn::Have.new(n, :at_least) end
def have_at_most(n)
### Warning:
expect("this").to have_at_most(4).letters
@example
Exactly like have() with <=.
def have_at_most(n) BuiltIn::Have.new(n, :at_most) end
def include(*expected)
expect("spread").to include("read")
expect([1,2,3]).not_to include(4)
expect([1,2,3]).to include(2,3,4) # fails
expect([1,2,3]).to include(2,3)
expect([1,2,3]).to include(3)
@example
and it will only pass if all args are found in collection.
collections and Strings. You can also pass in multiple args
Passes if actual includes expected. This works for
def include(*expected) BuiltIn::Include.new(*expected) end
def match(expected)
- Note: - Due to Ruby's method dispatch mechanism, using the `#match` matcher
def match(expected) BuiltIn::Match.new(expected) end
def match_array(array)
- Note: - This matcher only supports positive expectations.
Note: - This is also available using the `=~` operator with `should`,
def match_array(array) BuiltIn::MatchArray.new(array) end
def method_missing(method, *args, &block)
def method_missing(method, *args, &block) return Matchers::BuiltIn::BePredicate.new(method, *args, &block) if method.to_s =~ /^be_/ return Matchers::BuiltIn::Has.new(method, *args, &block) if method.to_s =~ /^have_/ super end
def raise_error(error=Exception, message=nil, &block)
expect { do_something_risky }.to raise_error(PoorRiskDecisionError, /oo ri/)
expect { do_something_risky }.to raise_error(PoorRiskDecisionError, "that was too risky")
expect { do_something_risky }.to raise_error(PoorRiskDecisionError) { |error| expect(error.data).to eq 42 }
expect { do_something_risky }.to raise_error(PoorRiskDecisionError)
expect { do_something_risky }.to raise_error
@example
Pass an optional block to perform extra verifications on the exception matched
With a named error and messsage specified as a Regexp, matches only if both match.
With a named error and messsage specified as a String, matches only if both match.
With a named error, matches only if that specific error is raised.
With no args, matches if any error is raised.
def raise_error(error=Exception, message=nil, &block) BuiltIn::RaiseError.new(error, message, &block) end
def respond_to(*names)
expect("string").to respond_to(:length)
@example
provided. Names can be Strings or Symbols.
Matches if the target object responds to all of the names
def respond_to(*names) BuiltIn::RespondTo.new(*names) end
def satisfy(&block)
@example
a custom matcher, which would likely make your specs more expressive.
If you do find yourself in such a situation, you could always write
specify.
you can't find any other way to specify the behaviour you wish to
Generally speaking, this should be thought of as a last resort when
block.
Passes if the submitted block returns true. Yields target to the
def satisfy(&block) BuiltIn::Satisfy.new(&block) end
def start_with(*expected)
expect([0, 1, 2, 3, 4]).to start_with 0
expect("this string").to start_with "this s"
@example
`expected.length` elements of the actual array.
of the actual string. In the case of an array, matches against the first
case of a string, matches against the first `expected.length` characters
Matches if the actual value starts with the expected value(s). In the
def start_with(*expected) BuiltIn::StartWith.new(*expected) end
def throw_symbol(expected_symbol=nil, expected_arg=nil)
expect { do_something_risky }.not_to throw_symbol(:that_was_risky)
expect { do_something_risky }.not_to throw_symbol
expect { do_something_risky }.to throw_symbol(:that_was_risky, 'culprit')
expect { do_something_risky }.to throw_symbol(:that_was_risky)
expect { do_something_risky }.to throw_symbol
@example
specified Symbol with the specified arg.
Given a Symbol and an arg, matches if the given proc throws the
Given a Symbol, matches if the given proc throws the specified Symbol.
Given no argument, matches if a proc throws any Symbol.
def throw_symbol(expected_symbol=nil, expected_arg=nil) BuiltIn::ThrowSymbol.new(expected_symbol, expected_arg) end
def yield_control
- Note: - This matcher is not designed for use with methods that yield
Note: - Your expect block must accept a parameter and pass it on to
def yield_control BuiltIn::YieldControl.new end
def yield_successive_args(*args)
- Note: - Your expect block must accept a parameter and pass it on to
def yield_successive_args(*args) BuiltIn::YieldSuccessiveArgs.new(*args) end
def yield_with_args(*args)
- Note: - This matcher is not designed for use with methods that yield
Note: - Your expect block must accept a parameter and pass it on to
def yield_with_args(*args) BuiltIn::YieldWithArgs.new(*args) end
def yield_with_no_args
- Note: - This matcher is not designed for use with methods that yield
Note: - Your expect block must accept a parameter and pass it on to
def yield_with_no_args BuiltIn::YieldWithNoArgs.new end