From 34ca4fdd8d06d41a6aea541df01889846024c5da Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Mon, 6 Jul 2020 16:03:08 +0200 Subject: [PATCH] Fix `global_zone` factory The factory linting spec is breaking many PR builds lately with this error: FactoryBot::InvalidFactoryError: The following factories are invalid: * global_zone - Validation failed: Name has already been taken (ActiveRecord::RecordInvalid) The problem is likely caused by the fact that the factory `global_zone` can be used only once, as the name is fixed to "Global Zone" and the model has a uniqueness validation on `name`. Still, I don't really understand why the problem happens, as the DB should be clean when this spec example is run. Anyway, the factory now uses the `initialize_with` strategy in order to avoid creating invalid records when the global zone already exists. --- core/lib/spree/testing_support/factories/zone_factory.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/spree/testing_support/factories/zone_factory.rb b/core/lib/spree/testing_support/factories/zone_factory.rb index 4bb30cef97c..c425c1982b8 100644 --- a/core/lib/spree/testing_support/factories/zone_factory.rb +++ b/core/lib/spree/testing_support/factories/zone_factory.rb @@ -5,7 +5,7 @@ FactoryBot.define do factory :global_zone, class: 'Spree::Zone' do - name { 'GlobalZone' } + initialize_with { Spree::Zone.find_or_initialize_by(name: 'GlobalZone') } zone_members do |proxy| zone = proxy.instance_eval { @instance } Spree::Country.all.map do |c|