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/FactoryAssociationWithStrategy false positive with keyword argument #73

Open
jcoyne opened this issue Sep 18, 2023 · 5 comments

Comments

@jcoyne
Copy link

jcoyne commented Sep 18, 2023

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
Copy link
Member

pirj commented Sep 18, 2023

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
Copy link
Author

jcoyne commented Sep 18, 2023

@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
Copy link
Member

pirj commented Sep 18, 2023

I see. Thanks for reporting. Might be a duplicate of #61, but not necessarily.

@Abdullah-l
Copy link

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

@pirj
Copy link
Member

pirj commented Aug 10, 2024

A PR is welcome, @Abdullah-l

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

3 participants