From c3a6c03395b98a5c69646232f4300fb1d246820a Mon Sep 17 00:00:00 2001 From: Alessandro Desantis Date: Fri, 24 Jul 2020 15:27:09 +0200 Subject: Use parent strategy when building associations in factories This avoids surprising scenarios where we expect a `create` call to result in all records being persisted to the DB, while some associated records are still unsaved. This was especially problematic when building subscription line items, because the order is retrieved through a `has_one :through` association on the `Spree::LineItem` record. If the line item isn't saved, the order cannot be retrieved, resulting in unexpected errors. --- .../testing_support/factories/installment_factory.rb | 6 +++--- .../testing_support/factories/line_item_factory.rb | 2 +- .../testing_support/factories/spree/line_item_factory.rb | 8 +++----- .../testing_support/factories/spree/order_factory.rb | 9 +++------ .../testing_support/factories/subscription_factory.rb | 2 +- 5 files changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/solidus_subscriptions/testing_support/factories/installment_factory.rb b/lib/solidus_subscriptions/testing_support/factories/installment_factory.rb index e8cea70..9914019 100644 --- a/lib/solidus_subscriptions/testing_support/factories/installment_factory.rb +++ b/lib/solidus_subscriptions/testing_support/factories/installment_factory.rb @@ -3,11 +3,11 @@ FactoryBot.define do transient { subscription_traits { [] } } - subscription { build :subscription, :with_line_item, *subscription_traits } + subscription { association(:subscription, :with_line_item, *subscription_traits) } trait :failed do actionable_date { Time.zone.yesterday } - details { build_list(:installment_detail, 1, installment: @instance) } + details { [association(:installment_detail, installment: @instance)] } end trait :success do @@ -16,7 +16,7 @@ FactoryBot.define do end details do - build_list(:installment_detail, 1, :success, installment: @instance, order: order) + [association(:installment_detail, :success, installment: @instance, order: order)] end end end diff --git a/lib/solidus_subscriptions/testing_support/factories/line_item_factory.rb b/lib/solidus_subscriptions/testing_support/factories/line_item_factory.rb index 3f67ed0..51926a6 100644 --- a/lib/solidus_subscriptions/testing_support/factories/line_item_factory.rb +++ b/lib/solidus_subscriptions/testing_support/factories/line_item_factory.rb @@ -12,7 +12,7 @@ FactoryBot.define do subscription_traits { [] } end - subscription { build :subscription, *subscription_traits } + subscription { association :subscription, *subscription_traits } end end end diff --git a/lib/solidus_subscriptions/testing_support/factories/spree/line_item_factory.rb b/lib/solidus_subscriptions/testing_support/factories/spree/line_item_factory.rb index c40cafa..c9d1012 100644 --- a/lib/solidus_subscriptions/testing_support/factories/spree/line_item_factory.rb +++ b/lib/solidus_subscriptions/testing_support/factories/spree/line_item_factory.rb @@ -6,11 +6,9 @@ FactoryBot.modify do end subscription_line_items do - build_list( - :subscription_line_item, - n_subscription_line_items, - spree_line_item: @instance - ) + Array.new(n_subscription_line_items) do + association :subscription_line_item, spree_line_item: @instance + end end end end diff --git a/lib/solidus_subscriptions/testing_support/factories/spree/order_factory.rb b/lib/solidus_subscriptions/testing_support/factories/spree/order_factory.rb index 4b373fb..3eca360 100644 --- a/lib/solidus_subscriptions/testing_support/factories/spree/order_factory.rb +++ b/lib/solidus_subscriptions/testing_support/factories/spree/order_factory.rb @@ -6,12 +6,9 @@ FactoryBot.modify do end line_items do - build_list( - :line_item, - n_line_items, - :with_subscription_line_items, - order: @instance - ) + Array.new(n_line_items) do + association :line_item, :with_subscription_line_items, order: @instance + end end end end diff --git a/lib/solidus_subscriptions/testing_support/factories/subscription_factory.rb b/lib/solidus_subscriptions/testing_support/factories/subscription_factory.rb index 87fe75d..8660b13 100644 --- a/lib/solidus_subscriptions/testing_support/factories/subscription_factory.rb +++ b/lib/solidus_subscriptions/testing_support/factories/subscription_factory.rb @@ -17,7 +17,7 @@ FactoryBot.define do line_item_traits { [] } end - line_items { build_list :subscription_line_item, 1, *line_item_traits } + line_items { [association(:subscription_line_item, *line_item_traits)] } end trait :with_shipping_address do -- cgit v1.2.3