Class: Fare::FareMatcher::Expectation

Inherits:
Object
  • Object
show all
Defined in:
lib/fare/rspec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, action, payload) ⇒ Expectation

Returns a new instance of Expectation.



18
19
20
21
22
# File 'lib/fare/rspec.rb', line 18

def initialize(subject, action, payload)
  @subject = subject
  @action  = action
  @payload = payload
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



16
17
18
# File 'lib/fare/rspec.rb', line 16

def action
  @action
end

#payloadObject (readonly)

Returns the value of attribute payload.



16
17
18
# File 'lib/fare/rspec.rb', line 16

def payload
  @payload
end

#subjectObject (readonly)

Returns the value of attribute subject.



16
17
18
# File 'lib/fare/rspec.rb', line 16

def subject
  @subject
end

Instance Method Details

#does_not_match?(expect_block) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


34
35
36
37
38
39
# File 'lib/fare/rspec.rb', line 34

def does_not_match?(expect_block)
  raise ArgumentError, "Checking payload with negative expectation is not possible" if payload
  Fare.stubbed_messages.clear
  expect_block.call
  event.nil?
end

#eventObject



69
70
71
# File 'lib/fare/rspec.rb', line 69

def event
  Fare.stubbed_messages.get(subject, action)
end

#event_nameObject



57
58
59
# File 'lib/fare/rspec.rb', line 57

def event_name
  "#{subject}##{action}"
end

#failure_messageObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fare/rspec.rb', line 41

def failure_message
  if event.nil?
    if Fare.stubbed_messages.size == 0
      "There were no events published"
    else
      "Expected event #{event_name}, but got:\n#{Fare.stubbed_messages.list}"
    end
  else
    "Payload did not match: #{payload_matcher.last_error}\nActual payload was:\n#{event.payload}"
  end
end

#failure_message_when_negatedObject



53
54
55
# File 'lib/fare/rspec.rb', line 53

def failure_message_when_negated
  "Expected not to publish #{event_name}, but published it with payload: #{event.payload}"
end

#matches?(expect_block) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/fare/rspec.rb', line 28

def matches?(expect_block)
  Fare.stubbed_messages.clear
  expect_block.call
  !event.nil? && payload_matches?
end

#payload_matcherObject



73
74
75
# File 'lib/fare/rspec.rb', line 73

def payload_matcher
  @payload_matcher ||= JsonExpressions::Matcher.new(payload)
end

#payload_matches?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
# File 'lib/fare/rspec.rb', line 61

def payload_matches?
  if payload
    payload_matcher =~ event.payload
  else
    true
  end
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/fare/rspec.rb', line 24

def supports_block_expectations?
  true
end