Class: Kookaburra::MentalModel::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/kookaburra/mental_model_matcher.rb

Overview

This is a custom matcher that matches the RSpec matcher API.

See Also:

Instance Method Summary collapse

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

#descriptionObject

(Part of the RSpec protocol for custom matchers.)

Returns:

  • String



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_nothingself

Reads better than #only with no args

Returns:

  • (self)


42
43
44
# File 'lib/kookaburra/mental_model_matcher.rb', line 42

def expecting_nothing
  only
end

#failure_message_for_shouldObject

Message to be printed when observed reality does not conform to mental model.

(Part of the RSpec protocol for custom matchers.)

Returns:

  • String



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kookaburra/mental_model_matcher.rb', line 65

def failure_message_for_should
  message = "expected #{@collection_key} to match the user's mental model, but:\n"
  unless expected_items_found?
    message += "expected to be present:         #{pp_array(expected_items)}\n"
    message += "the missing elements were:      #{pp_array(expected_items_that_were_not_found)}\n"
  end
  if unexpected_items_found?
    message += "expected to not be present:     #{pp_array(unexpected_items)}\n"
    message += "the unexpected extra elements:  #{pp_array(unexpected_items_that_were_found)}\n"
  end
  message
end

#failure_message_for_should_notObject

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.)

Returns:

  • String



86
87
88
# File 'lib/kookaburra/mental_model_matcher.rb', line 86

def failure_message_for_should_not
  "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.)

Parameters:

  • actual (Array)

    This is the data observed that you are attempting to match against the mental model.

Returns:

  • (Boolean)

    Boolean



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.

Parameters:

  • collection_keys (Array)

    The keys used in your mental model to reference the data

Returns:

  • (self)


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