Skip to content

Commit

Permalink
Fix global_zone factory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spaghetticode committed Jul 6, 2020
1 parent 08f6301 commit 34ca4fd
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion core/lib/spree/testing_support/factories/zone_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 34ca4fd

Please sign in to comment.