Module: Cased::TestHelper
- Defined in:
- lib/cased/test_helper.rb
Instance Method Summary collapse
- #after_teardown ⇒ Object
-
#assert_cased_events(expected_event_count, expected_event_body = nil, &block) ⇒ void
Assertion that helps with testing that a number of events have been published to Cased.
-
#assert_no_cased_events(expected_event_body = nil, &block) ⇒ void
Assertion that expects there to have been zero matching Cased events.
- #before_setup ⇒ Object
- #cased_events ⇒ Object
-
#cased_events_with(expected_event = {}) ⇒ Array<Hash>
Locates all published events matching a particular shape.
-
#cased_test_publisher ⇒ Cased::Publishers::TestPublisher
The test published used for the duration of the test.
- #clear_cased_context ⇒ Object
-
#clear_cased_events ⇒ Object
Clears all published events in the test Cased publisher.
Instance Method Details
#after_teardown ⇒ Object
18 19 20 21 22 |
# File 'lib/cased/test_helper.rb', line 18 def after_teardown super Cased.publishers = @original_cased_publishers end |
#assert_cased_events(expected_event_count, expected_event_body = nil, &block) ⇒ void
This method returns an undefined value.
Assertion that helps with testing that a number of events have been published to Cased.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cased/test_helper.rb', line 78 def assert_cased_events(expected_event_count, expected_event_body = nil, &block) expected_event_body&.deep_symbolize_keys! actual_event_count = if block events_before_block = cased_events_with(expected_event_body) block&.call events_after_block = cased_events_with(expected_event_body) events_after_block.length - events_before_block.length else cased_events_with(expected_event_body).length end assert_equal expected_event_count, actual_event_count, "#{expected_event_count} Cased published events expected, but #{actual_event_count} were published" end |
#assert_no_cased_events(expected_event_body = nil, &block) ⇒ void
This method returns an undefined value.
Assertion that expects there to have been zero matching Cased events.
129 130 131 |
# File 'lib/cased/test_helper.rb', line 129 def assert_no_cased_events(expected_event_body = nil, &block) assert_cased_events(0, expected_event_body, &block) end |
#before_setup ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/cased/test_helper.rb', line 7 def before_setup @original_cased_publishers = Cased.publishers Cased.publishers = [ cased_test_publisher, ] clear_cased_events clear_cased_context super end |
#cased_events ⇒ Object
33 34 35 |
# File 'lib/cased/test_helper.rb', line 33 def cased_events cased_test_publisher.events end |
#cased_events_with(expected_event = {}) ⇒ Array<Hash>
Locates all published events matching a particular shape.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cased/test_helper.rb', line 150 def cased_events_with(expected_event = {}) return cased_events.dup if expected_event.nil? if expected_event.empty? raise ArgumentError, 'You must call cased_events_with with a non empty Hash otherwise it will match all events' end = Cased::Context::Expander.(expected_event) if .empty? raise ArgumentError, <<~MSG.strip cased_events_with would have matched any published Cased event. cased_events_with was called with #{expected_event.inspect} but resulted into #{} after it was expanded. This typically happens when an object that includes Cased::Model does not implement either the #cased_id or #to_s method. MSG end # We need to normalize input as it could be a mix of strings and symbols. expected_event.deep_symbolize_keys! = .to_a events = cased_events.dup.collect(&:deep_symbolize_keys).collect(&:to_a) matching_events = events.select do |event| diff = - event diff.empty? end matching_events.collect(&:to_h) end |
#cased_test_publisher ⇒ Cased::Publishers::TestPublisher
The test published used for the duration of the test.
184 185 186 |
# File 'lib/cased/test_helper.rb', line 184 def cased_test_publisher @cased_test_publisher ||= Cased::Publishers::TestPublisher.new end |
#clear_cased_context ⇒ Object
29 30 31 |
# File 'lib/cased/test_helper.rb', line 29 def clear_cased_context Cased::Context.clear! end |
#clear_cased_events ⇒ Object
Clears all published events in the test Cased publisher
25 26 27 |
# File 'lib/cased/test_helper.rb', line 25 def clear_cased_events cased_events.clear end |