Module: RSpec::Matcher
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/rspec/matcher.rb,
lib/rspec/matcher/identity.rb
Overview
Provides minimal interface for creating RSpec Matchers.
Register matcher with RSpec via register_as
.
Include RSpec::Matchers::Composable
to support composable matchers.
Required Methods:
match
Optional Methods:
description
failure_message
failure_message_when_negated
diffable?
supports_block_expectations?
Defined Under Namespace
Modules: ClassMethods, Identity, PrependedMethods Classes: RescueAndCatch
Constant Summary collapse
- UNDEFINED =
To indicate no value was passed to matcher
Object.new.freeze
Instance Method Summary collapse
-
#actual ⇒ any
Value passed to
expect()
. -
#clean_up ⇒ void
Always gets called after decision is made.
-
#description ⇒ String
Describes what this matcher does.
-
#diffable? ⇒ Boolean
Indicates if actual and expected should be diffed on failure.
-
#expected ⇒ any
Value passed to matcher function.
-
#failure_message ⇒ String
Describe failure.
-
#failure_message_when_negated ⇒ String
Describe failure when not_to is used.
-
#initialize ⇒ Object
First argument passed to matcher is placed in expected and removed from arg list.
-
#match(_ = nil) ⇒ Boolean
Determines if there is a match or not.
-
#matches?(actual) ⇒ Boolean
private
Hides RSpec internal api.
-
#reject_expectation(message = UNDEFINED) ⇒ void
private
Stops evaluation and tells RSpec there wasn't a match.
-
#resolve_expectation ⇒ void
private
Stops evaluation and tells RSpec there was a match.
-
#supports_block_expectations? ⇒ Boolean
Indicates if actual can be a block.
-
#undefined? ⇒ Boolean
private
Indicates if expected was passed or not.
Instance Method Details
#actual ⇒ any
Returns value passed to expect()
.
|
# File 'lib/rspec/matcher.rb', line 149
|
#clean_up ⇒ void
This method returns an undefined value.
Always gets called after decision is made.
218 219 |
# File 'lib/rspec/matcher.rb', line 218 def clean_up end |
#description ⇒ String
for composable matchers
raises by default
used for failure messages
Describes what this matcher does.
177 178 179 |
# File 'lib/rspec/matcher.rb', line 177 def description raise "not implemented" end |
#diffable? ⇒ Boolean
false by default
Indicates if actual and expected should be diffed on failure.
204 205 206 |
# File 'lib/rspec/matcher.rb', line 204 def diffable? false end |
#expected ⇒ any
Returns value passed to matcher function.
|
# File 'lib/rspec/matcher.rb', line 153
|
#failure_message ⇒ String
raises by default
Describe failure.
186 187 188 189 190 |
# File 'lib/rspec/matcher.rb', line 186 def return "Expected #{expected} to #{description}" if == UNDEFINED "Expected #{expected} to #{description} but #{}" end |
#failure_message_when_negated ⇒ String
raises by default
Describe failure when not_to is used.
197 198 199 |
# File 'lib/rspec/matcher.rb', line 197 def .sub "to", "not to" end |
#initialize ⇒ Object
First argument passed to matcher is placed in expected and removed from arg list. Every argument beside that including a block is passed to initialize function.
|
# File 'lib/rspec/matcher.rb', line 157
|
#match(_ = nil) ⇒ Boolean
Must be implemented.
Determines if there is a match or not.
166 167 168 |
# File 'lib/rspec/matcher.rb', line 166 def match _ = nil raise "not implemented" end |
#matches?(actual) ⇒ Boolean
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.
Hides RSpec internal api
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rspec/matcher.rb', line 138 def matches? actual self.actual = actual RescueAndCatch.new self do catching(:resolution) do method(:match).arity == 0 ? match : match(actual) end ensuring(Exception) { clean_up } end.result end |
#reject_expectation(message = UNDEFINED) ⇒ void (private)
message is used in failure messages
This method returns an undefined value.
Stops evaluation and tells RSpec there wasn't a match.
237 238 239 240 |
# File 'lib/rspec/matcher.rb', line 237 def reject_expectation = UNDEFINED self. = throw :resolution, false end |
#resolve_expectation ⇒ void (private)
This method returns an undefined value.
Stops evaluation and tells RSpec there was a match.
227 228 229 |
# File 'lib/rspec/matcher.rb', line 227 def resolve_expectation throw :resolution, true end |
#supports_block_expectations? ⇒ Boolean
false by default
Indicates if actual can be a block.
212 213 214 |
# File 'lib/rspec/matcher.rb', line 212 def supports_block_expectations? false end |
#undefined? ⇒ Boolean (private)
Indicates if expected was passed or not
244 245 246 |
# File 'lib/rspec/matcher.rb', line 244 def undefined? expected == UNDEFINED end |