Skip to content

Commit

Permalink
Extract #with_events_disabled helper for reuse
Browse files Browse the repository at this point in the history
In a subsequent commit we want to use this outside of the reporting
subscriber tests.
  • Loading branch information
benjaminwil committed Oct 13, 2023
1 parent 10b19f0 commit 666dcbd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
5 changes: 2 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@

FactoryBot.reload

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f }
require "support/solidus_events_helper"

RSpec.configure do |config|
config.include SolidusEventsHelper
config.infer_spec_type_from_file_location!
config.use_transactional_fixtures = false

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
require "spec_helper"

# These tests will excercise either the `ReportinSubscriber` or the
# These tests will excercise either the `ReportingSubscriber` or the
# `LegacyReportingSubscriber` depending on the version of Solidus they are
# run against. The loading of the correct subscriber is handled by the
# initializer added by this extension's install generator.
RSpec.describe "ReportingSubscriber" do
# We only want to trigger the real event action behaviour as our spec
# `subject`s.
def with_events_disabled(&block)
if SolidusSupport::LegacyEventCompat.using_legacy?
allow(Spree::Event).to receive(:fire).and_return(nil)
else
allow(Spree::Bus).to receive(:publish).and_return(nil)
end

object = yield block

if SolidusSupport::LegacyEventCompat.using_legacy?
allow(Spree::Event).to receive(:fire).and_call_original
else
allow(Spree::Bus).to receive(:publish).and_call_original
end

object
end

before do
create(:taxjar_configuration, preferred_reporting_enabled_at_integer: reporting_enabled_at.to_i)
end
Expand Down
26 changes: 26 additions & 0 deletions spec/support/solidus_events_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module SolidusEventsHelper
# We can use this helper method to ensure that events are not run until after
# the test setup has been completed. This helps us more easily test side
# effects from events or control unintended side effects when attempting to
# test functionality that is not event-driven but would otherwise emit events.
#
# @yield Any test setup you'd like to run without events being emitted.
# @return The return value of the test setup in your block.
def with_events_disabled(&block)
if SolidusSupport::LegacyEventCompat.using_legacy?
allow(Spree::Event).to receive(:fire).and_return(nil)
else
allow(Spree::Bus).to receive(:publish).and_return(nil)
end

object = yield block

if SolidusSupport::LegacyEventCompat.using_legacy?
allow(Spree::Event).to receive(:fire).and_call_original
else
allow(Spree::Bus).to receive(:publish).and_call_original
end

object
end
end

0 comments on commit 666dcbd

Please sign in to comment.