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

Cop idea: flag callbacks for building associations #7

Open
pirj opened this issue Oct 29, 2022 · 0 comments
Open

Cop idea: flag callbacks for building associations #7

pirj opened this issue Oct 29, 2022 · 0 comments
Labels

Comments

@pirj
Copy link
Member

pirj commented Oct 29, 2022

I see this frequently:

  factory(:status_message) do
    author

    after(:build) do |sm|
      FactoryBot.create(:poll, status_message: sm)
    end

This won't work with build_stubbed.

The can be written as (see Interconnected Associations):

  factory(:status_message, aliases: %i[status_message_without_participation]) do
    author

    poll { assoiation :poll, status_message: instance }
  end

instance was added in FactoryBot 6.1.0, @instance can be used in older versions.

This also works for has_many:

    factory :user_with_five_posts do
      posts do
        Array.new(5) { association(:post) }
      end
    end
@ydah ydah transferred this issue from rubocop/rubocop-rspec May 6, 2023
@ydah ydah added the cop label May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants