Class: ActiveEventStore::TestHelper::EventPublishedMatcher
- Inherits:
-
Object
- Object
- ActiveEventStore::TestHelper::EventPublishedMatcher
- Defined in:
- lib/active_event_store/test_helper/event_published_matcher.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#matching_events ⇒ Object
readonly
Returns the value of attribute matching_events.
Class Method Summary collapse
- .event_data_matches?(attributes, event) ⇒ Boolean
- .event_matches?(event_class, attributes, event) ⇒ Boolean
- .event_type_matches?(event_class, event) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(expected_event_class, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, refute: false) ⇒ EventPublishedMatcher
constructor
A new instance of EventPublishedMatcher.
- #matches?(block) ⇒ Boolean
- #with_published_events(&block) ⇒ Object
Constructor Details
#initialize(expected_event_class, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, refute: false) ⇒ EventPublishedMatcher
Returns a new instance of EventPublishedMatcher.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 9 def initialize(expected_event_class, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, refute: false) @event_class = expected_event_class @store = store || ActiveEventStore.event_store @attributes = with @refute = refute count_expectations = { exactly: exactly, at_most: at_most, at_least: at_least }.reject { |_, v| v.nil? } if count_expectations.length > 1 raise ArgumentError("Only one of :exactly, :at_least or :at_most can be specified") elsif count_expectations.length == 0 @count_expectation_kind = :at_least @expected_count = 1 else @count_expectation_kind = count_expectations.keys.first @expected_count = count_expectations.values.first end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 6 def attributes @attributes end |
#matching_events ⇒ Object (readonly)
Returns the value of attribute matching_events.
6 7 8 |
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 6 def matching_events @matching_events end |
Class Method Details
.event_data_matches?(attributes, event) ⇒ Boolean
144 145 146 |
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 144 def event_data_matches?(attributes, event) attributes.nil? || attributes.all? { |k, v| v == event.public_send(k) } end |
.event_matches?(event_class, attributes, event) ⇒ Boolean
136 137 138 |
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 136 def event_matches?(event_class, attributes, event) event_type_matches?(event_class, event) && event_data_matches?(attributes, event) end |
.event_type_matches?(event_class, event) ⇒ Boolean
140 141 142 |
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 140 def event_type_matches?(event_class, event) event_class.identifier == event.event_type end |
Instance Method Details
#matches?(block) ⇒ Boolean
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 38 def matches?(block) raise ArgumentError, "#{assertion_name} only support block assertions" if block.nil? events = with_published_events do block.call end @matching_events, @unmatching_events = partition_events(events) = (@matching_events.size) if expectations = [ "Expected #{} #{@event_class.identifier}" ] expectations << if refute? report_events = @matching_events "not to have been published" else report_events = @unmatching_events "to have been published" end expectations << "with attributes #{attributes.inspect}" unless attributes.nil? expectations << expectations.pop + ", but" expectations << if report_events.any? report_events.inject("published the following events instead:") do |msg, event| msg + "\n #{event.inspect}" end else "hasn't published anything" end return expectations.join(" ") end nil end |
#with_published_events(&block) ⇒ Object
32 33 34 35 36 |
# File 'lib/active_event_store/test_helper/event_published_matcher.rb', line 32 def with_published_events(&block) original_count = @store.read.count block.call in_block_events(original_count, @store.read.count) end |