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)