rubocop / rubocop-factory_bot

Code style checking for factory_bot files.
https://docs.rubocop.org/rubocop-factory_bot
MIT License
44 stars 13 forks source link

FactoryBot/FactoryAssociationWithStrategy false positive with keyword argument #73

Open jcoyne opened 12 months ago

jcoyne commented 12 months ago
spec/factories/holdings.rb:7:14: C: FactoryBot/FactoryAssociationWithStrategy: Use an implicit, explicit or inline definition instead of hard coding a strategy for setting association within factory.
      item { build(:item, :available, location:) }
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I don't think there is another way to write this given that location references another transient:

FactoryBot.define do
  factory :holding do
    transient do
      location factory: :gre_stacks
      item { build(:item, :available, location:) }
    end

    ...
pirj commented 12 months ago

How does this work? Is it really transient? https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md#with-associations

You could still refer to ‘location’ if you used ‘association’ instead of ‘build’, right?

With this in mind, can this be written as:

FactoryBot.define do
  factory :holding do
    location factory: :gre_stacks
    item { association(:item, :available, location:) }
  end

?

jcoyne commented 12 months ago

@pirj item is not really a property of holding, it's a transient that is later used by initialize_with, so I don't think association will work.

pirj commented 12 months ago

I see. Thanks for reporting. Might be a duplicate of https://github.com/rubocop/rubocop-factory_bot/issues/61, but not necessarily.

Abdullah-l commented 1 month ago

This cop needs to be fixed not to bark inside transient blocks

pirj commented 1 month ago

A PR is welcome, @Abdullah-l