Class: Amigo::SpecHelpers::EventPublishedMatcher
- Inherits:
-
Object
- Object
- Amigo::SpecHelpers::EventPublishedMatcher
- Defined in:
- lib/amigo/spec_helpers.rb
Instance Attribute Summary collapse
-
#recorded_events ⇒ Object
readonly
Returns the value of attribute recorded_events.
Instance Method Summary collapse
- #add_matched(event, payload) ⇒ Object
- #add_missing(event, payload) ⇒ Object
- #and(another_eventname, *expected_payload) ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(eventname, expected_payload = []) ⇒ EventPublishedMatcher
constructor
A new instance of EventPublishedMatcher.
- #match_expected_events ⇒ Object
- #matches?(given_proc) ⇒ Boolean
- #on_publish_error(err) ⇒ Object
- #payloads_match?(expected, recorded) ⇒ Boolean
- #record_event(event) ⇒ Object
- #supports_block_expectations? ⇒ Boolean
- #with_payload(expected_payload) ⇒ Object
Constructor Details
#initialize(eventname, expected_payload = []) ⇒ EventPublishedMatcher
Returns a new instance of EventPublishedMatcher.
48 49 50 51 52 53 |
# File 'lib/amigo/spec_helpers.rb', line 48 def initialize(eventname, expected_payload=[]) @expected_events = [[eventname, expected_payload]] @recorded_events = [] @missing = [] @matched = [] end |
Instance Attribute Details
#recorded_events ⇒ Object (readonly)
Returns the value of attribute recorded_events.
46 47 48 |
# File 'lib/amigo/spec_helpers.rb', line 46 def recorded_events @recorded_events end |
Instance Method Details
#add_matched(event, payload) ⇒ Object
120 121 122 |
# File 'lib/amigo/spec_helpers.rb', line 120 def add_matched(event, payload) @matched << [event, payload] end |
#add_missing(event, payload) ⇒ Object
124 125 126 |
# File 'lib/amigo/spec_helpers.rb', line 124 def add_missing(event, payload) @missing << [event, payload] end |
#and(another_eventname, *expected_payload) ⇒ Object
55 56 57 58 |
# File 'lib/amigo/spec_helpers.rb', line 55 def and(another_eventname, *expected_payload) @expected_events << [another_eventname, expected_payload] return self end |
#failure_message ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/amigo/spec_helpers.rb', line 128 def return "Error while publishing: %p" % [@error] if @error = [] @missing.each do |event, payload| = "expected a '%s' event to be fired" % [event] << (" with a payload of %p" % [payload]) unless payload.nil? << " but none was." << end if @recorded_events.empty? << "No events were sent." else parts = @recorded_events.map(&:inspect) << ("The following events were recorded: %s" % [parts.join(", ")]) end return .join("\n") end |
#failure_message_when_negated ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/amigo/spec_helpers.rb', line 151 def = [] @matched.each do |event, _payload| = "expected a '%s' event not to be fired" % [event] << (" with a payload of %p" % [@expected_payload]) if @expected_payload << " but one was." << end return .join("\n") end |
#match_expected_events ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/amigo/spec_helpers.rb', line 101 def match_expected_events @expected_events.each do |expected_event, expected_payload| match = @recorded_events.find do |recorded| recorded.name == expected_event && self.payloads_match?(expected_payload, recorded.payload) end if match self.add_matched(expected_event, expected_payload) else self.add_missing(expected_event, expected_payload) end end end |
#matches?(given_proc) ⇒ Boolean
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/amigo/spec_helpers.rb', line 76 def matches?(given_proc) unless given_proc.respond_to?(:call) warn "publish matcher used with non-proc object #{given_proc.inspect}" return false end unless Amigo.synchronous_mode warn "publish matcher used without synchronous_mode (use :async test metadata)" return false end state = {on_error: self.method(:on_publish_error), subscribers: [self.method(:record_event)]} Amigo::SpecHelpers.snapshot_async_state(state) do given_proc.call end self.match_expected_events return @error.nil? && @missing.empty? end |
#on_publish_error(err) ⇒ Object
97 98 99 |
# File 'lib/amigo/spec_helpers.rb', line 97 def on_publish_error(err) @error = err end |
#payloads_match?(expected, recorded) ⇒ Boolean
115 116 117 118 |
# File 'lib/amigo/spec_helpers.rb', line 115 def payloads_match?(expected, recorded) return expected.matches?(recorded) if expected.respond_to?(:matches?) return expected.nil? || expected.empty? || expected == recorded end |
#record_event(event) ⇒ Object
67 68 69 70 |
# File 'lib/amigo/spec_helpers.rb', line 67 def record_event(event) Amigo.log nil, :debug, "recording_event", event: event @recorded_events << event end |
#supports_block_expectations? ⇒ Boolean
72 73 74 |
# File 'lib/amigo/spec_helpers.rb', line 72 def supports_block_expectations? true end |
#with_payload(expected_payload) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/amigo/spec_helpers.rb', line 60 def with_payload(expected_payload) raise ArgumentError, "expected payload must be an array or matcher" unless expected_payload.is_a?(Array) || expected_payload.respond_to?(:matches?) @expected_events.last[1] = expected_payload return self end |