Module: RSpec::Matchers
- Included in:
- DSL::Matcher
- Defined in:
- lib/rspec/matchers.rb,
lib/rspec/matchers/dsl.rb,
lib/rspec/matchers/pretty.rb,
lib/rspec/matchers/built_in.rb,
lib/rspec/matchers/composable.rb,
lib/rspec/matchers/built_in/be.rb,
lib/rspec/matchers/built_in/eq.rb,
lib/rspec/matchers/built_in/all.rb,
lib/rspec/matchers/built_in/eql.rb,
lib/rspec/matchers/built_in/has.rb,
lib/rspec/matchers/built_in/match.rb,
lib/rspec/matchers/built_in/cover.rb,
lib/rspec/matchers/built_in/yield.rb,
lib/rspec/matchers/built_in/exist.rb,
lib/rspec/matchers/built_in/equal.rb,
lib/rspec/matchers/built_in/output.rb,
lib/rspec/matchers/aliased_matcher.rb,
lib/rspec/matchers/built_in/change.rb,
lib/rspec/matchers/matcher_protocol.rb,
lib/rspec/matchers/built_in/include.rb,
lib/rspec/matchers/built_in/satisfy.rb,
lib/rspec/matchers/built_in/compound.rb,
lib/rspec/matchers/matcher_delegator.rb,
lib/rspec/matchers/built_in/operators.rb,
lib/rspec/matchers/built_in/be_within.rb,
lib/rspec/matchers/built_in/respond_to.rb,
lib/rspec/matchers/built_in/be_between.rb,
lib/rspec/matchers/built_in/be_kind_of.rb,
lib/rspec/matchers/built_in/raise_error.rb,
lib/rspec/matchers/built_in/throw_symbol.rb,
lib/rspec/matchers/built_in/base_matcher.rb,
lib/rspec/matchers/generated_descriptions.rb,
lib/rspec/matchers/built_in/be_instance_of.rb,
lib/rspec/matchers/built_in/have_attributes.rb,
lib/rspec/matchers/built_in/contain_exactly.rb,
lib/rspec/matchers/built_in/start_and_end_with.rb
Overview
RSpec::Matchers provides a number of useful matchers we use to define expectations. Any object that implements the matcher protocol can be used as a matcher.
Predicates
In addition to 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 expect(..).to be_
followed by the predicate
without the question mark, and RSpec will figure it out from there.
For example:
expect([]).to be_empty # => [].empty?() | passes
expect([]).not_to be_empty # => [].empty?() | fails
In addtion to prefixing the predicate matchers with "be", you can also use "be_a" and "bean", making your specs read much more naturally:
expect("a string").to be_an_instance_of(String) # =>"a string".instance_of?(String) # passes
expect(3).to be_a_kind_of(Fixnum) # => 3.kind_of?(Numeric) | passes
expect(3).to be_a_kind_of(Numeric) # => 3.kind_of?(Numeric) | passes
expect(3).to be_an_instance_of(Fixnum) # => 3.instance_of?(Fixnum) | passes
expect(3).not_to be_an_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:
expect(:a => "A").to have_key(:a)
expect(:a => "A").to have_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.
Note that RSpec does not provide composable aliases for these dynamic predicate matchers. You can easily define your own aliases, though:
RSpec::Matchers.alias_matcher :a_user_who_is_an_admin, :be_an_admin
expect(user_list).to include(a_user_who_is_an_admin)
Custom Matchers
When you find that none of the stock 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:
expect(bob.current_zone).to eql(Zone.new("4"))
But you might find it more expressive to say:
expect(bob).to be_in_zone("4")
and/or
expect(bob).not_to 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
do |player|
# generate and return the appropriate string.
end
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 and failure_message_when_negated) are passed the actual value (the receiver of expect(..) or expect(..).not_to).
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
"expected #{@target.inspect} to be in Zone #{@expected}"
end
def
"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
Making custom matchers composable
RSpec's built-in matchers are designed to be composed, in expressions like:
expect(["barn", 2.45]).to contain_exactly(
a_value_within(0.1).of(2.5),
a_string_starting_with("bar")
)
Custom matchers can easily participate in composed matcher expressions like these.
Include Composable in your custom matcher to make it support
being composed (matchers defined using the DSL have this included automatically).
Within your matcher's matches?
method (or the match
block, if using the DSL),
use values_match?(expected, actual)
rather than expected == actual
.
Under the covers, values_match?
is able to match arbitrary
nested data structures containing a mix of both matchers and non-matcher objects.
It uses ===
and ==
to perform the matching, considering the values to
match if either returns true
. The Composable
mixin also provides some helper
methods for surfacing the matcher descriptions within your matcher's description
or failure messages.
RSpec's built-in matchers each have a number of aliases that rephrase the matcher
from a verb phrase (such as be_within
) to a noun phrase (such as a_value_within
),
which reads better when the matcher is passed as an argument in a composed matcher
expressions, and also uses the noun-phrase wording in the matcher's description
,
for readable failure messages. You can alias your custom matchers in similar fashion
using Matchers.alias_matcher.
Defined Under Namespace
Modules: BuiltIn, Composable, DSL, Pretty Classes: AliasedMatcher, MatcherProtocol
Constant Summary
Class Method Summary (collapse)
-
+ (Object) alias_matcher(new_name, old_name, options = {}) {|String| ... }
Defines a matcher alias.
-
+ (Object) clear_generated_description
private
Used by rspec-core to clear the state used to generate descriptions after an example.
-
+ (RSpec::Expectations::Configuration) configuration
Delegates to Expectations.configuration.
-
+ (Object) define_negated_matcher(negated_name, base_name) {|String| ... }
Defines a negated matcher.
-
+ (Object) generated_description
private
Generates an an example description based on the last expectation.
Instance Method Summary (collapse)
-
- (Object) all(expected)
Passes if actual all expected objects pass.
-
- (Object) be(*args)
(also: #a_value)
Given true, false, or nil, will pass if actual value is true, false or nil (respectively).
-
- (Object) be_a(klass)
(also: #be_an)
passes if target.kind_of?(klass).
-
- (Object) be_a_kind_of(expected)
(also: #be_kind_of, #a_kind_of)
Passes if actual.kind_of?(expected).
-
- (Object) be_an_instance_of(expected)
(also: #be_instance_of, #an_instance_of)
Passes if actual.instance_of?(expected).
-
- (Object) be_between(min, max)
(also: #a_value_between)
Passes if actual.between?(min, max).
-
- (Object) be_falsey
(also: #be_falsy, #a_falsey_value, #a_falsy_value)
Passes if actual is falsey (false or nil).
-
- (Object) be_nil
(also: #a_nil_value)
Passes if actual is nil.
-
- (Object) be_truthy
(also: #a_truthy_value)
Passes if actual is truthy (anything but false or nil).
-
- (Object) be_within(delta)
(also: #a_value_within, #within)
Passes if actual == expected +/- delta.
-
- (Object) change(receiver = nil, message = nil, &block)
(also: #a_block_changing, #changing)
Applied to a proc, specifies that its execution will cause some value to change.
-
- (Object) contain_exactly(*items)
(also: #a_collection_containing_exactly, #containing_exactly)
Passes if actual contains all of the expected regardless of order.
-
- (Object) cover(*values)
(also: #a_range_covering, #covering)
Passes if actual covers expected.
-
- (Object) end_with(*expected)
(also: #a_collection_ending_with, #a_string_ending_with, #ending_with)
Matches if the actual value ends with the expected value(s).
-
- (Object) eq(expected)
(also: #an_object_eq_to, #eq_to)
Passes if actual == expected.
-
- (Object) eql(expected)
(also: #an_object_eql_to, #eql_to)
Passes if
actual.eql?(expected)
. -
- (Object) equal(expected)
(also: #an_object_equal_to, #equal_to)
Passes if actual.equal?(expected) (object identity).
-
- (Object) exist(*args)
(also: #an_object_existing, #existing)
Passes if
actual.exist?
oractual.exists?
. -
- (ExpectationTarget) expect
Supports
expect(actual).to matcher
syntax by wrappingactual
in anExpectationTarget
. -
- (Object) have_attributes(expected)
(also: #an_object_having_attributes)
Passes if actual's attribute values match the expected attributes hash.
-
- (Object) include(*expected)
(also: #a_collection_including, #a_string_including, #a_hash_including, #including)
Passes if actual includes expected.
-
- (Object) match(expected)
(also: #match_regex, #an_object_matching, #a_string_matching, #matching)
Given a
Regexp
orString
, passes ifactual.match(pattern)
Given an arbitrary nested data structure (e.g. arrays and hashes), matches ifexpected === actual
||actual == expected
for each pair of elements. -
- (Object) match_array(items)
An alternate form of
contain_exactly
that accepts the expected contents as a single array arg rather that splatted out as individual items. -
- (Object) output(expected = nil)
(also: #a_block_outputting)
With no arg, passes if the block outputs
to_stdout
orto_stderr
. -
- (Object) raise_error(error = Exception, message = nil, &block)
(also: #raise_exception, #a_block_raising, #raising)
With no args, matches if any error is raised.
-
- (Object) respond_to(*names)
(also: #an_object_responding_to, #responding_to)
Matches if the target object responds to all of the names provided.
-
- (Object) satisfy(&block)
(also: #an_object_satisfying, #satisfying)
Passes if the submitted block returns true.
-
- (Object) start_with(*expected)
(also: #a_collection_starting_with, #a_string_starting_with, #starting_with)
Matches if the actual value starts with the expected value(s).
-
- (Object) throw_symbol(expected_symbol = nil, expected_arg = nil)
(also: #a_block_throwing, #throwing)
Given no argument, matches if a proc throws any Symbol.
-
- (Object) yield_control
(also: #a_block_yielding_control, #yielding_control)
Passes if the method called in the expect block yields, regardless of whether or not arguments are yielded.
-
- (Object) yield_successive_args(*args)
(also: #a_block_yielding_successive_args, #yielding_successive_args)
Designed for use with methods that repeatedly yield (such as iterators).
-
- (Object) yield_with_args(*args)
(also: #a_block_yielding_with_args, #yielding_with_args)
Given no arguments, matches if the method called in the expect block yields with arguments (regardless of what they are or how many there are).
-
- (Object) yield_with_no_args
(also: #a_block_yielding_with_no_args, #yielding_with_no_args)
Passes if the method called in the expect block yields with no arguments.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(method, *args, &block) (private)
931 932 933 934 935 936 937 938 939 940 |
# File 'lib/rspec/matchers.rb', line 931 def method_missing(method, *args, &block) case method.to_s when BE_PREDICATE_REGEX BuiltIn::BePredicate.new(method, *args, &block) when HAS_REGEX BuiltIn::Has.new(method, *args, &block) else super end end |
Class Method Details
+ (Object) alias_matcher(new_name, old_name, options = {}) {|String| ... }
Defines a matcher alias. The returned matcher's description
will be overriden
to reflect the phrasing of the new name, which will be used in failure messages
when passed as an argument to another matcher in a composed matcher expression.
246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/rspec/matchers.rb', line 246 def self.alias_matcher(new_name, old_name, ={}, &description_override) description_override ||= lambda do |old_desc| old_desc.gsub(Pretty.split_words(old_name), Pretty.split_words(new_name)) end klass = .fetch(:klass) { AliasedMatcher } define_method(new_name) do |*args, &block| matcher = __send__(old_name, *args, &block) klass.new(matcher, description_override) end end |
+ (Object) clear_generated_description
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Used by rspec-core to clear the state used to generate descriptions after an example.
11 12 13 14 |
# File 'lib/rspec/matchers/generated_descriptions.rb', line 11 def self.clear_generated_description self.last_matcher = nil self.last_expectation_handler = nil end |
+ (RSpec::Expectations::Configuration) configuration
Delegates to Expectations.configuration.
This is here because rspec-core's expect_with
option
looks for a configuration
method on the mixin
(RSpec::Matchers
) to yield to a block.
922 923 924 |
# File 'lib/rspec/matchers.rb', line 922 def self.configuration Expectations.configuration end |
+ (Object) define_negated_matcher(negated_name, base_name) {|String| ... }
Defines a negated matcher. The returned matcher's description
and failure_message
will be overriden to reflect the phrasing of the new name, and the match logic will
be based on the original matcher but negated.
274 275 276 |
# File 'lib/rspec/matchers.rb', line 274 def self.define_negated_matcher(negated_name, base_name, &description_override) alias_matcher(negated_name, base_name, :klass => AliasedNegatedMatcher, &description_override) end |
+ (Object) generated_description
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generates an an example description based on the last expectation. Used by rspec-core's one-liner syntax.
19 20 21 22 |
# File 'lib/rspec/matchers/generated_descriptions.rb', line 19 def self.generated_description return nil if last_expectation_handler.nil? "#{last_expectation_handler.verb} #{last_description}" end |
Instance Method Details
- (Object) all(expected)
The negative form not_to all
is not supported. Instead
use not_to include
or pass a negative form of a matcher
as the argument (e.g. all exclude(:foo)
).
You can also use this with compound matchers as well.
Passes if actual all expected objects pass. This works for any enumerable object.
635 636 637 |
# File 'lib/rspec/matchers.rb', line 635 def all(expected) BuiltIn::All.new(expected) end |
- (Object) be(*args) Also known as: a_value
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 arbitrarypredicate feature will handle any predicate prefixed with "be_an" (e.g. bean_instance_of), "be_a" (e.g. bea_kind_of) or "be" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
318 319 320 |
# File 'lib/rspec/matchers.rb', line 318 def be(*args) args.empty? ? Matchers::BuiltIn::Be.new : equal(*args) end |
- (Object) be_a(klass) Also known as: be_an
passes if target.kind_of?(klass)
324 325 326 |
# File 'lib/rspec/matchers.rb', line 324 def be_a(klass) be_a_kind_of(klass) end |
- (Object) be_a_kind_of(expected) Also known as: be_kind_of, a_kind_of
Passes if actual.kind_of?(expected)
349 350 351 |
# File 'lib/rspec/matchers.rb', line 349 def be_a_kind_of(expected) BuiltIn::BeAKindOf.new(expected) end |
- (Object) be_an_instance_of(expected) Also known as: be_instance_of, an_instance_of
Passes if actual.instance_of?(expected)
336 337 338 |
# File 'lib/rspec/matchers.rb', line 336 def be_an_instance_of(expected) BuiltIn::BeAnInstanceOf.new(expected) end |
- (Object) be_between(min, max) Also known as: a_value_between
Passes if actual.between?(min, max). Works with any Comparable object, including String, Symbol, Time, or Numeric (Fixnum, Bignum, Integer, Float, Complex, and Rational).
By default, be_between
is inclusive (i.e. passes when given either the max or min value),
but you can make it exclusive
by chaining that off the matcher.
367 368 369 |
# File 'lib/rspec/matchers.rb', line 367 def be_between(min, max) BuiltIn::BeBetween.new(min, max) end |
- (Object) be_falsey Also known as: be_falsy, a_falsey_value, a_falsy_value
Passes if actual is falsey (false or nil)
285 286 287 |
# File 'lib/rspec/matchers.rb', line 285 def be_falsey BuiltIn::BeFalsey.new end |
- (Object) be_nil Also known as: a_nil_value
Passes if actual is nil
293 294 295 |
# File 'lib/rspec/matchers.rb', line 293 def be_nil BuiltIn::BeNil.new end |
- (Object) be_truthy Also known as: a_truthy_value
Passes if actual is truthy (anything but false or nil)
279 280 281 |
# File 'lib/rspec/matchers.rb', line 279 def be_truthy BuiltIn::BeTruthy.new end |
- (Object) be_within(delta) Also known as: a_value_within, within
Passes if actual == expected +/- delta
378 379 380 |
# File 'lib/rspec/matchers.rb', line 378 def be_within(delta) BuiltIn::BeWithin.new(delta) end |
- (Object) change(receiver = nil, message = nil, &block) Also known as: a_block_changing, changing
Applied to a proc, specifies that its execution will cause some value to change.
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 expect(..).to
or expect(...).not_to
method.
You can chain any of the following off of the end to specify details about the change:
from
to
or any one of:
by
by_at_least
by_at_most
== Notes
Evaluates receiver.message
or block
before and after it
evaluates the block passed to expect
.
expect( ... ).not_to change
supports the form that specifies from
(which specifies what you expect the starting, unchanged value to be)
but does not support forms with subsequent calls to by
, by_at_least
,
by_at_most
or to
.
463 464 465 |
# File 'lib/rspec/matchers.rb', line 463 def change(receiver=nil, =nil, &block) BuiltIn::Change.new(receiver, , &block) end |
- (Object) contain_exactly(*items) Also known as: a_collection_containing_exactly, containing_exactly
This is also available using the =~
operator with should
,
but =~
is not supported with expect
.
Passes if actual contains all of the expected regardless of order. This works for collections. Pass in multiple args and it will only pass if all args are found in collection.
482 483 484 |
# File 'lib/rspec/matchers.rb', line 482 def contain_exactly(*items) BuiltIn::ContainExactly.new(items) end |
- (Object) cover(*values) Also known as: a_range_covering, covering
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.
Warning:: Ruby >= 1.9 only
500 501 502 |
# File 'lib/rspec/matchers.rb', line 500 def cover(*values) BuiltIn::Cover.new(*values) end |
- (Object) end_with(*expected) Also known as: a_collection_ending_with, a_string_ending_with, ending_with
Matches if the actual value ends with the expected value(s). In the case
of a string, matches against the last expected.length
characters of the
actual string. In the case of an array, matches against the last
expected.length
elements of the actual array.
516 517 518 |
# File 'lib/rspec/matchers.rb', line 516 def end_with(*expected) BuiltIn::EndWith.new(*expected) end |
- (Object) eq(expected) Also known as: an_object_eq_to, eq_to
Passes if actual == expected.
See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
532 533 534 |
# File 'lib/rspec/matchers.rb', line 532 def eq(expected) BuiltIn::Eq.new(expected) end |
- (Object) eql(expected) Also known as: an_object_eql_to, eql_to
Passes if actual.eql?(expected)
See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
547 548 549 |
# File 'lib/rspec/matchers.rb', line 547 def eql(expected) BuiltIn::Eql.new(expected) end |
- (Object) equal(expected) Also known as: an_object_equal_to, equal_to
Passes if actual.equal?(expected) (object identity).
See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
562 563 564 |
# File 'lib/rspec/matchers.rb', line 562 def equal(expected) BuiltIn::Equal.new(expected) end |
- (Object) exist(*args) Also known as: an_object_existing, existing
Passes if actual.exist?
or actual.exists?
572 573 574 |
# File 'lib/rspec/matchers.rb', line 572 def exist(*args) BuiltIn::Exist.new(*args) end |
- (ExpectationTarget) expect
Supports expect(actual).to matcher
syntax by wrapping actual
in an
ExpectationTarget
.
|
# File 'lib/rspec/matchers.rb', line 205
|
- (Object) have_attributes(expected) Also known as: an_object_having_attributes
It will fail if actual doesn't respond to any of the expected attributes.
Passes if actual's attribute values match the expected attributes hash. This works no matter how you define your attribute readers.
594 595 596 |
# File 'lib/rspec/matchers.rb', line 594 def have_attributes(expected) BuiltIn::HaveAttributes.new(expected) end |
- (Object) include(*expected) Also known as: a_collection_including, a_string_including, a_hash_including, including
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.
611 612 613 |
# File 'lib/rspec/matchers.rb', line 611 def include(*expected) BuiltIn::Include.new(*expected) end |
- (Object) match(expected) Also known as: match_regex, an_object_matching, a_string_matching, matching
The match_regex
alias is deprecated and is not recommended for use.
It was added in 2.12.1 to facilitate its use from within custom
matchers (due to how the custom matcher DSL was evaluated in 2.x,
match
could not be used there), but is no longer needed in 3.x.
Given a Regexp
or String
, passes if actual.match(pattern)
Given an arbitrary nested data structure (e.g. arrays and hashes),
matches if expected === actual
|| actual == expected
for each
pair of elements.
672 673 674 |
# File 'lib/rspec/matchers.rb', line 672 def match(expected) BuiltIn::Match.new(expected) end |
- (Object) match_array(items)
An alternate form of contain_exactly
that accepts
the expected contents as a single array arg rather
that splatted out as individual items.
691 692 693 |
# File 'lib/rspec/matchers.rb', line 691 def match_array(items) contain_exactly(*items) end |
- (Object) output(expected = nil) Also known as: a_block_outputting
This matcher works by temporarily replacing $stdout
or $stderr
,
so it's not able to intercept stream output that explicitly uses STDOUT
/STDERR
or that uses a reference to $stdout
/$stderr
that was stored before the
matcher is used.
With no arg, passes if the block outputs to_stdout
or to_stderr
.
With a string, passes if the blocks outputs that specific string to_stdout
or to_stderr
.
With a regexp or matcher, passes if the blocks outputs a string to_stdout
or to_stderr
that matches.
717 718 719 |
# File 'lib/rspec/matchers.rb', line 717 def output(expected=nil) BuiltIn::Output.new(expected) end |
- (Object) raise_error(error = Exception, message = nil, &block) Also known as: raise_exception, a_block_raising, raising
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
737 738 739 |
# File 'lib/rspec/matchers.rb', line 737 def raise_error(error=Exception, =nil, &block) BuiltIn::RaiseError.new(error, , &block) end |
- (Object) respond_to(*names) Also known as: an_object_responding_to, responding_to
Matches if the target object responds to all of the names provided. Names can be Strings or Symbols.
757 758 759 |
# File 'lib/rspec/matchers.rb', line 757 def respond_to(*names) BuiltIn::RespondTo.new(*names) end |
- (Object) satisfy(&block) Also known as: an_object_satisfying, satisfying
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.
776 777 778 |
# File 'lib/rspec/matchers.rb', line 776 def satisfy(&block) BuiltIn::Satisfy.new(&block) end |
- (Object) start_with(*expected) Also known as: a_collection_starting_with, a_string_starting_with, starting_with
Matches if the actual value starts with the expected value(s). In the
case of a string, matches against the first expected.length
characters
of the actual string. In the case of an array, matches against the first
expected.length
elements of the actual array.
792 793 794 |
# File 'lib/rspec/matchers.rb', line 792 def start_with(*expected) BuiltIn::StartWith.new(*expected) end |
- (Object) throw_symbol(expected_symbol = nil, expected_arg = nil) Also known as: a_block_throwing, throwing
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.
815 816 817 |
# File 'lib/rspec/matchers.rb', line 815 def throw_symbol(expected_symbol=nil, expected_arg=nil) BuiltIn::ThrowSymbol.new(expected_symbol, expected_arg) end |
- (Object) yield_control Also known as: a_block_yielding_control, yielding_control
Your expect block must accept a parameter and pass it on to the method-under-test as a block.
This matcher is not designed for use with methods that yield multiple times.
Passes if the method called in the expect block yields, regardless of whether or not arguments are yielded.
839 840 841 |
# File 'lib/rspec/matchers.rb', line 839 def yield_control BuiltIn::YieldControl.new end |
- (Object) yield_successive_args(*args) Also known as: a_block_yielding_successive_args, yielding_successive_args
Your expect block must accept a parameter and pass it on to the method-under-test as a block.
Designed for use with methods that repeatedly yield (such as iterators). Passes if the method called in the expect block yields multiple times with arguments matching those given.
Argument matching is done using ===
(the case match operator)
and ==
. If the expected and actual arguments match with either
operator, the matcher will pass.
911 912 913 |
# File 'lib/rspec/matchers.rb', line 911 def yield_successive_args(*args) BuiltIn::YieldSuccessiveArgs.new(*args) end |
- (Object) yield_with_args(*args) Also known as: a_block_yielding_with_args, yielding_with_args
Your expect block must accept a parameter and pass it on to the method-under-test as a block.
This matcher is not designed for use with methods that yield multiple times.
Given no arguments, matches if the method called in the expect block yields with arguments (regardless of what they are or how many there are).
Given arguments, matches if the method called in the expect block yields with arguments that match the given arguments.
Argument matching is done using ===
(the case match operator)
and ==
. If the expected and actual arguments match with either
operator, the matcher will pass.
889 890 891 |
# File 'lib/rspec/matchers.rb', line 889 def yield_with_args(*args) BuiltIn::YieldWithArgs.new(*args) end |
- (Object) yield_with_no_args Also known as: a_block_yielding_with_no_args, yielding_with_no_args
Your expect block must accept a parameter and pass it on to the method-under-test as a block.
This matcher is not designed for use with methods that yield multiple times.
Passes if the method called in the expect block yields with no arguments. Fails if it does not yield, or yields with arguments.
858 859 860 |
# File 'lib/rspec/matchers.rb', line 858 def yield_with_no_args BuiltIn::YieldWithNoArgs.new end |