Module: RSpec::Matchers::DSL::Macros
- Included in:
- Matcher
- Defined in:
- lib/rspec/matchers/dsl.rb
Overview
Contains the methods that are available from within the
RSpec::Matchers.define
DSL for creating custom matchers.
Defined Under Namespace
Modules: Deprecated
Instance Method Summary (collapse)
-
- (Object) chain(method_name, *attr_names, &definition)
Convenience for defining methods on this matcher to create a fluent interface.
-
- (Object) description {|Object| ... }
Customize the description to use for one-liners.
-
- (Object) diffable
Tells the matcher to diff the actual and expected values in the failure message.
-
- (Object) failure_message {|Object| ... }
Customizes the failure messsage to use when this matcher is asked to positively match.
-
- (Object) failure_message_when_negated {|Object| ... }
Customize the failure messsage to use when this matcher is asked to negatively match.
-
- (Object) match {|Object| ... }
Stores the block that is used to determine whether this matcher passes or fails.
-
- (Object) match_unless_raises(expected_exception = Exception) {|Object| ... }
Use this instead of
match
when the block will raise an exception rather than returning false to indicate a failure. -
- (Object) match_when_negated {|Object| ... }
Use this to define the block for a negative expectation (
expect(...).not_to
) when the positive and negative forms require different handling. -
- (Object) supports_block_expectations
Declares that the matcher can be used in a block expectation.
Instance Method Details
- (Object) chain(method_name, *attr_names, &definition)
Convenience for defining methods on this matcher to create a fluent
interface. The trick about fluent interfaces is that each method must
return self in order to chain methods together. chain
handles that
for you. If the method is invoked and the
include_chain_clauses_in_custom_matcher_descriptions
config option
hash been enabled, the chained method name and args will be added to the
default description and failure message.
In the common case where you just want the chained method to store some
value(s) for later use (e.g. in match
), you can provide one or more
attribute names instead of a block; the chained method will store its
arguments in instance variables with those names, and the values will
be exposed via getters.
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/rspec/matchers/dsl.rb', line 205 def chain(method_name, *attr_names, &definition) unless block_given? ^ attr_names.any? raise ArgumentError, "You must pass either a block or some attribute names (but not both) to `chain`." end definition = assign_attributes(attr_names) if attr_names.any? define_user_override(method_name, definition) do |*args, &block| super(*args, &block) @chained_method_clauses.push([method_name, args]) self end end |
- (Object) description {|Object| ... }
Customize the description to use for one-liners. Only use this when the description generated by default doesn't suit your needs.
160 161 162 |
# File 'lib/rspec/matchers/dsl.rb', line 160 def description(&definition) define_user_override(__method__, definition) end |
- (Object) diffable
Tells the matcher to diff the actual and expected values in the failure message.
166 167 168 |
# File 'lib/rspec/matchers/dsl.rb', line 166 def diffable define_method(:diffable?) { true } end |
- (Object) failure_message {|Object| ... }
Customizes the failure messsage to use when this matcher is asked to positively match. Only use this when the message generated by default doesn't suit your needs.
123 124 125 |
# File 'lib/rspec/matchers/dsl.rb', line 123 def (&definition) define_user_override(__method__, definition) end |
- (Object) failure_message_when_negated {|Object| ... }
Customize the failure messsage to use when this matcher is asked to negatively match. Only use this when the message generated by default doesn't suit your needs.
142 143 144 |
# File 'lib/rspec/matchers/dsl.rb', line 142 def (&definition) define_user_override(__method__, definition) end |
- (Object) match {|Object| ... }
Stores the block that is used to determine whether this matcher passes
or fails. The block should return a boolean value. When the matcher is
passed to expect(...).to
and the block returns true
, then the expectation
passes. Similarly, when the matcher is passed to expect(...).not_to
and the
block returns false
, then the expectation passes.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rspec/matchers/dsl.rb', line 57 def match(&match_block) define_user_override(:matches?, match_block) do |actual| begin @actual = actual super(*actual_arg_for(match_block)) rescue RSpec::Expectations::ExpectationNotMetError false end end end |
- (Object) match_unless_raises(expected_exception = Exception) {|Object| ... }
Use this instead of match
when the block will raise an exception
rather than returning false to indicate a failure.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rspec/matchers/dsl.rb', line 95 def match_unless_raises(expected_exception=Exception, &match_block) define_user_override(:matches?, match_block) do |actual| @actual = actual begin super(*actual_arg_for(match_block)) rescue expected_exception => @rescued_exception false else true end end end |
- (Object) match_when_negated {|Object| ... }
Use this to define the block for a negative expectation (expect(...).not_to
)
when the positive and negative forms require different handling. This
is rarely necessary, but can be helpful, for example, when specifying
asynchronous processes that require different timeouts.
74 75 76 77 78 79 |
# File 'lib/rspec/matchers/dsl.rb', line 74 def match_when_negated(&match_block) define_user_override(:does_not_match?, match_block) do |actual| @actual = actual super(*actual_arg_for(match_block)) end end |
- (Object) supports_block_expectations
Declares that the matcher can be used in a block expectation.
Users will not be able to use your matcher in a block
expectation without declaring this.
(e.g. expect { do_something }.to matcher
).
174 175 176 |
# File 'lib/rspec/matchers/dsl.rb', line 174 def supports_block_expectations define_method(:supports_block_expectations?) { true } end |