From cd601319f3a1ce0bc49864c9bb590aec9cfaa781 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 7 Aug 2023 19:05:00 +0100 Subject: [PATCH] Fix factory issues --- spec/factories/product_factory.rb | 8 +++++++- spec/factories/variant_factory.rb | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb index 320060563f..8e1d5e64c7 100644 --- a/spec/factories/product_factory.rb +++ b/spec/factories/product_factory.rb @@ -3,13 +3,18 @@ FactoryBot.define do factory :base_product, class: Spree::Product do sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" } + + transient do + primary_taxon { nil } + end + + primary_taxon_id { |p| (p.primary_taxon || Spree::Taxon.first || create(:taxon)).id } description { generate(:random_description) } price { 19.99 } sku { 'ABC' } deleted_at { nil } supplier { Enterprise.is_primary_producer.first || FactoryBot.create(:supplier_enterprise) } - primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) } unit_value { 1 } unit_description { '' } @@ -48,6 +53,7 @@ FactoryBot.define do on_demand { false } on_hand { 5 } end + after(:create) do |product, evaluator| product.variants.first.on_demand = evaluator.on_demand product.variants.first.on_hand = evaluator.on_hand diff --git a/spec/factories/variant_factory.rb b/spec/factories/variant_factory.rb index cd4b322a02..ec2ebf8a4d 100644 --- a/spec/factories/variant_factory.rb +++ b/spec/factories/variant_factory.rb @@ -11,7 +11,8 @@ FactoryBot.define do width { generate(:random_float) } depth { generate(:random_float) } - product { |p| p.association(:base_product) } + primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) } + product { |v| create(:product, primary_taxon_id: v.primary_taxon.id) } # ensure stock item will be created for this variant before(:create) { create(:stock_location) if Spree::StockLocation.count.zero? } @@ -22,7 +23,7 @@ FactoryBot.define do on_hand { 5 } end - product { |p| p.association(:product) } + product { |v| create(:product, primary_taxon_id: v.primary_taxon.id) } unit_value { 1 } unit_description { '' }