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
-
#chain(name, &definition) ⇒ Object
Convenience for defining methods on this matcher to create a fluent interface.
-
#description {|Object| ... } ⇒ Object
Customize the description to use for one-liners.
-
#diffable ⇒ Object
Tells the matcher to diff the actual and expected values in the failure message.
-
#failure_message {|Object| ... } ⇒ Object
Customizes the failure messsage to use when this matcher is asked to positively match.
-
#failure_message_when_negated {|Object| ... } ⇒ Object
Customize the failure messsage to use when this matcher is asked to negatively match.
-
#match {|Object| ... } ⇒ Object
Stores the block that is used to determine whether this matcher passes or fails.
-
#match_unless_raises(expected_exception = Exception) {|Object| ... } ⇒ Object
Use this instead of
matchwhen the block will raise an exception rather than returning false to indicate a failure. -
#match_when_negated {|Object| ... } ⇒ Object
Use this to define the block for a negative expectation (
expect(...).not_to) when the positive and negative forms require different handling. -
#supports_block_expectations ⇒ Object
Declares that the matcher can be used in a block expectation.
Instance Method Details
#chain(name, &definition) ⇒ Object
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.
180 181 182 183 184 185 |
# File 'lib/rspec/matchers/dsl.rb', line 180 def chain(name, &definition) define_user_override(name, definition) do |*args, &block| super(*args, &block) self end end |
#description {|Object| ... } ⇒ Object
Customize the description to use for one-liners. Only use this when the description generated by default doesn't suit your needs.
144 145 146 |
# File 'lib/rspec/matchers/dsl.rb', line 144 def description(&definition) define_user_override(__method__, definition) end |
#diffable ⇒ Object
Tells the matcher to diff the actual and expected values in the failure message.
150 151 152 |
# File 'lib/rspec/matchers/dsl.rb', line 150 def diffable define_method(:diffable?) { true } end |
#failure_message {|Object| ... } ⇒ 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.
107 108 109 |
# File 'lib/rspec/matchers/dsl.rb', line 107 def (&definition) define_user_override(__method__, definition) end |
#failure_message_when_negated {|Object| ... } ⇒ 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.
126 127 128 |
# File 'lib/rspec/matchers/dsl.rb', line 126 def (&definition) define_user_override(__method__, definition) end |
#match {|Object| ... } ⇒ 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.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rspec/matchers/dsl.rb', line 41 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 |
#match_unless_raises(expected_exception = Exception) {|Object| ... } ⇒ Object
Use this instead of match when the block will raise an exception
rather than returning false to indicate a failure.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rspec/matchers/dsl.rb', line 79 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 |
#match_when_negated {|Object| ... } ⇒ 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.
58 59 60 61 62 63 |
# File 'lib/rspec/matchers/dsl.rb', line 58 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 |
#supports_block_expectations ⇒ Object
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).
158 159 160 |
# File 'lib/rspec/matchers/dsl.rb', line 158 def supports_block_expectations define_method(:supports_block_expectations?) { true } end |