Bring product factory from spree_core and merge with modification

This commit is contained in:
Luis Ramos
2020-08-23 13:52:24 +01:00
parent ee65b4ba27
commit aa00756f9c

View File

@@ -1,4 +1,46 @@
FactoryBot.define do
factory :base_product, class: Spree::Product do
sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" }
description { generate(:random_description) }
price 19.99
cost_price 17.00
sku 'ABC'
available_on { 1.year.ago }
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 ''
variant_unit 'weight'
variant_unit_scale 1
variant_unit_name ''
shipping_category { |r| Spree::ShippingCategory.first || r.association(:shipping_category) }
# ensure stock item will be created for this products master
before(:create) { create(:stock_location) if Spree::StockLocation.count == 0 }
factory :product do
transient do
on_hand { 5 }
end
tax_category { |r| Spree::TaxCategory.first || r.association(:tax_category) }
after(:create) do |product, evaluator|
product.master.on_hand = evaluator.on_hand
product.variants.first.on_hand = evaluator.on_hand
end
factory :product_with_option_types do
after(:create) { |product| create(:product_option_type, product: product) }
end
end
end
factory :product_with_image, parent: :product do
after(:create) do |product|
image = File.open(Rails.root.join('app', 'assets', 'images', 'logo-white.png'))
@@ -42,30 +84,3 @@ FactoryBot.define do
end
end
end
FactoryBot.modify do
factory :product do
transient do
on_hand { 5 }
end
primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) }
after(:create) do |product, evaluator|
product.master.on_hand = evaluator.on_hand
product.variants.first.on_hand = evaluator.on_hand
end
end
factory :base_product do
supplier { Enterprise.is_primary_producer.first || FactoryBot.create(:supplier_enterprise) }
primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) }
unit_value 1
unit_description ''
variant_unit 'weight'
variant_unit_scale 1
variant_unit_name ''
end
end