Fix factory issues

This commit is contained in:
Matt-Yorkley
2023-08-07 19:05:00 +01:00
committed by Gaetan Craig-Riou
parent 3b715875ad
commit cd601319f3
2 changed files with 10 additions and 3 deletions

View File

@@ -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

View File

@@ -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 { '' }