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.EnforcedStyle: Explicit adds offence when using traits in factories #54

Closed
jgrau opened this issue Jun 14, 2023 · 1 comment · Fixed by #59
Labels
bug Something isn't working

Comments

@jgrau
Copy link

jgrau commented Jun 14, 2023

Hi. First of all thanks for this gem :)

I have this factory

FactoryBot.define do
  factory :booking do
   #... lots of unimportant stuff removed

    trait :with_booker_invoice do
      #... unimportant stuff removed
    end

    trait :with_host_invoice do
      #... unimportant stuff removed
    end

    trait :with_invoices do
      with_booker_invoice
      with_host_invoice
    end
  end
end

and this configuration

FactoryBot/AssociationStyle:
  EnforcedStyle: explicit

Rubocop now warns me on the lines with with_booker_invoice and with_host_invoice:

FactoryBot/AssociationStyle: Use explicit style to define associations. (https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/AssociationStyle)RuboCop(FactoryBot/AssociationStyle)

AFAIK this it the only way to define traits within traits (https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md#traits-within-traits) but the problem also exits when using traits in the factory eg.

FactoryBot.define do
  factory :listing do
    trait :seasonal do
      pricing_scheme { :seasonal }

      daily_price_low_season_cents { 500_00 }
      daily_price_high_season_cents { 1000_00 }
      daily_price_weekday_cents { nil }
      daily_price_weekend_cents { nil }

      cleaning_fee_cents { 500_00 }
    end

    trait :flat do
      pricing_scheme { :flat }

      daily_price_weekday_cents { 500_00 }
      daily_price_weekend_cents { 1000_00 }
      daily_price_low_season_cents { nil }
      daily_price_high_season_cents { nil }

      cleaning_fee_cents { 500_00 }
    end

    # Default to the flat pricing scheme
    flat
  end
@znz
Copy link

znz commented Jul 5, 2023

I met this issue too.

root@8e2c142c74c8:/tmp# cat .rubocop.yml
AllCops:
  NewCops: enable

FactoryBot/AssociationStyle:
  EnforcedStyle: explicit
root@8e2c142c74c8:/tmp# cat f.rb
FactoryBot.define do
  factory :order do
    trait :completed do
      completed_at { 3.days.ago }
    end

    trait :refunded do
      completed
      refunded_at { 1.day.ago }
    end
  end
end
root@8e2c142c74c8:/tmp# cp f.rb spec/factories.rb
root@8e2c142c74c8:/tmp# rubocop -r rubocop-factory_bot --only FactoryBot/AssociationStyle -A spec/factories.rb
Inspecting 1 file
C

Offenses:

spec/factories.rb:8:7: C: [Corrected] FactoryBot/AssociationStyle: Use explicit style to define associations.
      completed
      ^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
root@8e2c142c74c8:/tmp# cat spec/factories.rb
FactoryBot.define do
  factory :order do
    trait :completed do
      completed_at { 3.days.ago }
    end

    trait :refunded do
      association :completed
      refunded_at { 1.day.ago }
    end
  end
end

@ydah ydah added the bug Something isn't working label Jul 5, 2023
ydah added a commit that referenced this issue Jul 5, 2023
…Style: Explicit` and using trait within trait

Fix: #54
ydah added a commit that referenced this issue Aug 7, 2023
…Style: Explicit` and using trait within trait

Fix: #54
ydah added a commit that referenced this issue Aug 13, 2023
…Style: Explicit` and using trait within trait

Fix: #54
ydah added a commit that referenced this issue Aug 13, 2023
…Style: Explicit` and using trait within trait

Fix: #54
ydah added a commit that referenced this issue Aug 25, 2023
…Style: Explicit` and using trait within trait

Fix: #54
@ydah ydah closed this as completed in #59 Sep 3, 2023
ydah added a commit that referenced this issue Sep 7, 2023
…Style: Explicit` and using trait within trait

Fix: #54
ydah added a commit that referenced this issue Sep 7, 2023
…Style: Explicit` and using trait within trait

Fix: #54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants