Module: Spree::TestingSupport::BusHelpers
- Extended by:
- RSpec::Matchers::DSL
- Defined in:
- lib/spree/testing_support/bus_helpers.rb
Overview
RSpec test helpers for the event bus
If you want to use the methods defined in this module, include it in your specs:
or, globally, in your ‘spec_helper.rb`:
Instance Method Summary collapse
-
#have_been_published(event_name) ⇒ Object
Matcher to test that an event has been published via Spree::Bus#publish.
-
#stub_spree_bus ⇒ Object
Stubs Bus.
Instance Method Details
#have_been_published(event_name) ⇒ Object
Matcher to test that an event has been published via Spree::Bus#publish
Before using this matcher, you need to call #stub_spree_bus.
Remember that the event listeners won’t be performed.
It can be chain through ‘with` to match with the published payload:
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/spree/testing_support/bus_helpers.rb', line 73 matcher :have_been_published do chain :with, :payload match do |expected_event| expected_event = normalize_name(expected_event) arguments = payload ? [expected_event, payload] : [expected_event, any_args] expect(Spree::Bus).to have_received(:publish).with(*arguments) end do |expected_event| <<~MSG expected #{expected_event.inspect} to have been published. Make sure that provided payload, if any, also matches. MSG end def normalize_name(event_name) if event_name.is_a?(Symbol) eq(event_name) else raise ArgumentError, <<~MSG "#{event_name.inspect} is not a valid event name. It must be a Symbol." MSG end end end |
#stub_spree_bus ⇒ Object
Stubs Bus
After you have called this method in an example, Bus will no longer publish any event for the duration of that example. All the method invocations on it will be stubbed.
Internally, it stubs Spree::Bus#publish.
After you call this method, probably you’ll want to call some of the matchers defined in this module.
41 42 43 |
# File 'lib/spree/testing_support/bus_helpers.rb', line 41 def stub_spree_bus allow(Spree::Bus).to receive(:publish) end |