diff --git a/core/lib/spree/testing_support/dummy_app.rb b/core/lib/spree/testing_support/dummy_app.rb index 39a246fb416..f9e9ed64f8b 100644 --- a/core/lib/spree/testing_support/dummy_app.rb +++ b/core/lib/spree/testing_support/dummy_app.rb @@ -41,6 +41,12 @@ def self.setup(gem_root:, lib_name:, auto_migrate: true) DummyApp::Application.config.root = root DummyApp::Application.initialize! unless DummyApp::Application.initialized? + # Raise on deprecation warnings. + # NOTE: This needs to happen after the application is initialized. + if ENV['SOLIDUS_RAISE_DEPRECATIONS'].present? + Spree.deprecator.behavior = :raise + end + if auto_migrate DummyApp::Migrations.auto_migrate end @@ -151,8 +157,3 @@ class Application < ::Rails::Application config.taxon_attachment_module = 'Spree::Taxon::PaperclipAttachment' end end - -# Raise on deprecation warnings -if ENV['SOLIDUS_RAISE_DEPRECATIONS'].present? - Spree.deprecator.behavior = :raise -end diff --git a/core/spec/lib/spree/deprecator_spec.rb b/core/spec/lib/spree/deprecator_spec.rb new file mode 100644 index 00000000000..1007b074f4c --- /dev/null +++ b/core/spec/lib/spree/deprecator_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe "Spree.deprecator" do + Dummy = Class.new do + def deprecated_method + "foo" + end + deprecate :deprecated_method, deprecator: Spree.deprecator + end + + context "by default" do + it "returns a valid deprecator" do + expect(Spree.deprecator).to have_attributes( + deprecation_horizon: "5.0", + gem_name: "Solidus" + ) + end + + it "does not raise an error unless overridden by environment" do + if ENV["SOLIDUS_RAISE_DEPRECATIONS"] + expect { Dummy.new.deprecated_method }.to raise_error(ActiveSupport::DeprecationException) + else + expect { Dummy.new.deprecated_method }.not_to raise_error + end + end + end + + context "when the behavior has been changed to :raise" do + around do |example| + behavior_name = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS.detect { |_behavior_name, behavior_proc| + behavior_proc == Spree.deprecator.behavior.first + }.first + + Spree.deprecator.behavior = :raise + + example.run + + Spree.deprecator.behavior = behavior_name + end + + it "raises an error when a deprecated method is called" do + expect { Dummy.new.deprecated_method } + .to raise_error(ActiveSupport::DeprecationException) + end + end +end