From f60a79437e3429a543487f1b7195c16560ec1faf Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 26 Jun 2024 10:56:15 +1000 Subject: [PATCH] Reworked the test to use variant factory Creating a variant actually create an extra one via the associated product, as it will create a "standard variant". As far as I can see there is no way around it, but it should be fixed once the Product refactor is finished, and product becomes product group. Added a comment on the variant factory to explain the problem. It's not ideal as it will slow down the test suite a little, but I think it's better to write the code the way you would expect it, and it will eventually get fixed. --- spec/factories/variant_factory.rb | 9 +++++-- .../products_and_inventory_report_spec.rb | 24 ++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/spec/factories/variant_factory.rb b/spec/factories/variant_factory.rb index 50da2702b0..dc729a29e6 100644 --- a/spec/factories/variant_factory.rb +++ b/spec/factories/variant_factory.rb @@ -14,7 +14,13 @@ FactoryBot.define do primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) } supplier { Enterprise.is_primary_producer.first || FactoryBot.create(:supplier_enterprise) } - product { |p| p.association(:base_product) } + # createing a product here will end up creating an extra variant, as creating product will + # create a "standard variant" by default. We could try to pass the variant instance we + # are creating but it fails because then the variant instance gets saved and it fails because + # the product isn't associated yet. It's a chicken and egg problem. + # It will be fixed once we finish the product refactor, and we don't need the product to + # create a "standard variant" + product { association :base_product } # ensure stock item will be created for this variant before(:create) { create(:stock_location) if Spree::StockLocation.count.zero? } @@ -25,7 +31,6 @@ FactoryBot.define do on_hand { 5 } end - product { |p| p.association(:product) } unit_value { 1 } unit_description { '' } diff --git a/spec/lib/reports/products_and_inventory_report_spec.rb b/spec/lib/reports/products_and_inventory_report_spec.rb index ff8025d4e6..19a7024bb1 100644 --- a/spec/lib/reports/products_and_inventory_report_spec.rb +++ b/spec/lib/reports/products_and_inventory_report_spec.rb @@ -267,12 +267,10 @@ module Reporting let(:report) do AllProducts.new user, { fields_to_hide: [] } end - # Creating a variant directly would create a product with another variant with no supplier - # which breaks the test - let(:variant) { create(:product).variants.first } + let(:variant) { create(:variant, supplier:) } let(:supplier) { create(:supplier_enterprise) } - it "Should return headers" do + it "returns headers" do expect(report.table_headers).to eq([ "Supplier", "Producer Suburb", @@ -289,29 +287,27 @@ module Reporting ]) end - it "Should render 'On demand' when the product is available on demand" do + it "renders 'On demand' when the product is available on demand" do variant.on_demand = true variant.on_hand = 15 - variant.supplier = supplier variant.save! - first_row = report.table_rows.first - on_demand_column = first_row[-2] - on_hand_column = first_row[-1] + last_row = report.table_rows.last + on_demand_column = last_row[-2] + on_hand_column = last_row[-1] expect(on_demand_column).to eq("Yes") expect(on_hand_column).to eq("On demand") end - it "Should render the on hand count when the product is not available on demand" do + it "renders the on hand count when the product is not available on demand" do variant.on_demand = false variant.on_hand = 22 - variant.supplier = supplier variant.save! - first_row = report.table_rows.first - on_demand_column = first_row[-2] - on_hand_column = first_row[-1] + last_row = report.table_rows.last + on_demand_column = last_row[-2] + on_hand_column = last_row[-1] expect(on_demand_column).to eq("No") expect(on_hand_column).to eq(22)