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

FactoryBot/AssociationStyle autocorrects to incorrect code #82

Open
a-lavis opened this issue Oct 25, 2023 · 1 comment
Open

FactoryBot/AssociationStyle autocorrects to incorrect code #82

a-lavis opened this issue Oct 25, 2023 · 1 comment

Comments

@a-lavis
Copy link

a-lavis commented Oct 25, 2023

Consider the following file:

# frozen_string_literal: true

FactoryBot.define do
  factory :post do
    association :user, strategy: :create
  end
end

This triggers a FactoryBot/AssociationStyle offense.
When I run rubocop -A on the file, I see:

Inspecting 1 file
C

Offenses:

test/factories/posts.rb:5:5: C: [Corrected] FactoryBot/AssociationStyle: Use implicit style to define associations.
    association :user, strategy: :create
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test/factories/posts.rb:5:5: C: [Corrected] FactoryBot/AttributeDefinedStatically: Use a block to declare attribute values.
    user strategy: :create
    ^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses corrected

The resulting file is:

# frozen_string_literal: true

FactoryBot.define do
  factory :post do
    user { { strategy: :create } }
  end
end

This defines user to be a hash with the key :strategy and the value :create, instead of an association to a User model that is created. This is not equivalent to association :user, strategy: :create.

Instead, should we be autocorrecting this code to the following?

# frozen_string_literal: true

FactoryBot.define do
  factory :post do
    user { association :user, strategy: :create }
  end
end

Or should we not trigger an offense at all?


Here is my .rubocop.yml:

require:
  - rubocop-factory_bot

AllCops:
  NewCops: enable
  TargetRubyVersion: 3.2

I am using rubocop 1.57.1 and rubocop-factory_bot 2.24.0.

@ydakuka
Copy link

ydakuka commented Oct 26, 2023

duplicate #79

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

No branches or pull requests

2 participants