rubocop-factory_bot
rubocop-factory_bot copied to clipboard
FactoryBot/FactoryAssociationWithStrategy false positive with keyword argument
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
...
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
?
@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.
I see. Thanks for reporting. Might be a duplicate of https://github.com/rubocop/rubocop-factory_bot/issues/61, but not necessarily.
This cop needs to be fixed not to bark inside transient blocks
A PR is welcome, @Abdullah-l