diff --git a/core/lib/spree/testing_support/factories/promotion_factory.rb b/core/lib/spree/testing_support/factories/promotion_factory.rb index 88cb6665d1e..8ca243af8ce 100644 --- a/core/lib/spree/testing_support/factories/promotion_factory.rb +++ b/core/lib/spree/testing_support/factories/promotion_factory.rb @@ -22,23 +22,40 @@ end trait :with_action do - after(:create) do |promotion, _evaluator| - promotion.actions << Spree::Promotion::Actions::CreateAdjustment.new + transient do + promotion_action_class { Spree::Promotion::Actions::CreateAdjustment } + end + + after(:create) do |promotion, evaluator| + promotion.actions << evaluator.promotion_action_class.new end end - trait :with_line_item_adjustment do + trait :with_adjustable_action do transient do - adjustment_rate { 10 } + preferred_amount { 10 } + calculator_class { Spree::Calculator::FlatRate } + promotion_action_class { Spree::Promotion::Actions::CreateItemAdjustments } end after(:create) do |promotion, evaluator| - calculator = Spree::Calculator::FlatRate.new - calculator.preferred_amount = evaluator.adjustment_rate - Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator: calculator, promotion: promotion) + calculator = evaluator.calculator_class.new + calculator.preferred_amount = evaluator.preferred_amount + evaluator.promotion_action_class.create!(calculator: calculator, promotion: promotion) end end + factory :promotion_with_action_adjustment, traits: [:with_adjustable_action] + + trait :with_line_item_adjustment do + transient do + adjustment_rate { 10 } + end + + with_adjustable_action + preferred_amount { adjustment_rate } + end + factory :promotion_with_item_adjustment, traits: [:with_line_item_adjustment] trait :with_free_shipping do @@ -52,14 +69,11 @@ weighted_order_adjustment_amount { 10 } end - after(:create) do |promotion, evaluator| - calculator = Spree::Calculator::FlatRate.new - calculator.preferred_amount = evaluator.weighted_order_adjustment_amount - action = Spree::Promotion::Actions::CreateAdjustment.create!(calculator: calculator) - promotion.actions << action - promotion.save! - end + with_adjustable_action + preferred_amount { weighted_order_adjustment_amount } + promotion_action_class { Spree::Promotion::Actions::CreateAdjustment } end + factory :promotion_with_order_adjustment, traits: [:with_order_adjustment] trait :with_item_total_rule do diff --git a/core/spec/lib/spree/core/testing_support/factories/promotion_factory_spec.rb b/core/spec/lib/spree/core/testing_support/factories/promotion_factory_spec.rb index 197b78dcd3e..efef29758df 100644 --- a/core/spec/lib/spree/core/testing_support/factories/promotion_factory_spec.rb +++ b/core/spec/lib/spree/core/testing_support/factories/promotion_factory_spec.rb @@ -11,6 +11,12 @@ it_behaves_like 'a working factory' end + describe 'promotion with action adjustment' do + let(:factory) { :promotion_with_action_adjustment } + + it_behaves_like 'a working factory' + end + describe 'promotion with item adjustment' do let(:factory) { :promotion_with_item_adjustment }