Class: Kookaburra::MentalModel::Matcher
- Inherits:
-
Object
- Object
- Kookaburra::MentalModel::Matcher
- Defined in:
- lib/kookaburra/mental_model_matcher.rb
Overview
This is a custom matcher that matches the RSpec matcher API.
Instance Method Summary collapse
-
#description ⇒ Object
(Part of the RSpec protocol for custom matchers.).
-
#expecting_nothing ⇒ self
Reads better than #only with no args.
-
#failure_message_for_should ⇒ Object
Message to be printed when observed reality does not conform to mental model.
-
#failure_message_for_should_not ⇒ Object
Message to be printed when observed reality does conform to mental model, but you did not expect it to.
-
#initialize(mental_model, collection_key) ⇒ Matcher
constructor
A new instance of Matcher.
-
#matches?(actual) ⇒ Boolean
The result contains everything that was expected to be found and nothing that was unexpected.
-
#only(*collection_keys) ⇒ self
Specifies that result should be limited to the given keys from the mental model.
Constructor Details
#initialize(mental_model, collection_key) ⇒ Matcher
Returns a new instance of Matcher.
10 11 12 13 14 15 16 17 |
# File 'lib/kookaburra/mental_model_matcher.rb', line 10 def initialize(mental_model, collection_key) @collection_key = collection_key mental_model.send(collection_key).tap do |collection| @expected = collection @unexpected = collection.deleted end end |
Instance Method Details
#description ⇒ Object
(Part of the RSpec protocol for custom matchers.)
93 94 95 |
# File 'lib/kookaburra/mental_model_matcher.rb', line 93 def description "match the user's mental model of #{@collection_key}" end |
#expecting_nothing ⇒ self
Reads better than #only with no args
42 43 44 |
# File 'lib/kookaburra/mental_model_matcher.rb', line 42 def expecting_nothing only end |
#failure_message_for_should ⇒ Object
Message to be printed when observed reality does not conform to mental model.
(Part of the RSpec protocol for custom matchers.)
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/kookaburra/mental_model_matcher.rb', line 65 def = "expected #{@collection_key} to match the user's mental model, but:\n" unless expected_items_found? += "expected to be present: #{pp_array(expected_items)}\n" += "the missing elements were: #{pp_array(expected_items_that_were_not_found)}\n" end if unexpected_items_found? += "expected to not be present: #{pp_array(unexpected_items)}\n" += "the unexpected extra elements: #{pp_array(unexpected_items_that_were_found)}\n" end end |
#failure_message_for_should_not ⇒ Object
Message to be printed when observed reality does conform to mental model, but you did not expect it to. (To be honest, we can't think of why you would want this, but it is included for the sake of RSpec compatibility.)
(Part of the RSpec protocol for custom matchers.)
86 87 88 |
# File 'lib/kookaburra/mental_model_matcher.rb', line 86 def "expected #{@collection_key} not to match the user's mental model" end |
#matches?(actual) ⇒ Boolean
The result contains everything that was expected to be found and nothing that was unexpected.
(Part of the RSpec protocol for custom matchers.)
54 55 56 57 |
# File 'lib/kookaburra/mental_model_matcher.rb', line 54 def matches?(actual) @actual = actual expected_items_found? && !unexpected_items_found? end |
#only(*collection_keys) ⇒ self
Specifies that result should be limited to the given keys from the mental model.
Useful if you are looking at a filtered result. That is, your mental model contains elements { A, B, C }, but you only expect to see element A.
29 30 31 32 33 34 35 36 37 |
# File 'lib/kookaburra/mental_model_matcher.rb', line 29 def only(*collection_keys) keepers = @expected.slice(*collection_keys) tossers = @expected.except(*collection_keys) @expected = keepers @unexpected.merge! tossers self end |