Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not attempt to create multiple records with global_zone factory #3688

Merged
merged 1 commit into from
Jul 7, 2020

Conversation

spaghetticode
Copy link
Member

Description

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)

I think this has something to do with 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.

Checklist:

@spaghetticode spaghetticode self-assigned this Jul 1, 2020
Copy link
Member

@jarednorman jarednorman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably linting the shipping method factory first and creating the global zone.

I'm okay with this change, but maybe the global_zone factory should use a find_or_initialize strategy instead so it can be created multiple times and return the original record.

@aldesantis
Copy link
Member

How about this?

factory :global_zone do
  initialize_with { Spree::Zone.find_or_initialize_by(name: name) }
  # ...
end

@spaghetticode
Copy link
Member Author

@aldesantis thank you, that's exactly what I was looking for but somehow I was not able to find it... this seems to be such a better solution. I'm updating the PR.

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.
@spaghetticode spaghetticode changed the title Do not lint global_zone factory Do not attempt to create multiple records with global_zone factory Jul 6, 2020
@kennyadsl kennyadsl merged commit 8b37c63 into solidusio:master Jul 7, 2020
@kennyadsl kennyadsl deleted the spaghetticode/flaky-spec branch July 7, 2020 09:15
@softr8
Copy link
Contributor

softr8 commented Jul 7, 2020

ah! I had to do the same in this commit: 3180cbf

Will rebase from master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants