rubocop / rubocop-rspec

Code style checking for RSpec files.
https://docs.rubocop.org/rubocop-rspec
MIT License
794 stars 272 forks source link

FactoryBot/AssociationStyle behavior with nested traits #1872

Closed jaydorsey closed 2 months ago

jaydorsey commented 2 months ago

I ran the corrector w/ this cop on our codebase, using the explicit style. It made one correction which broke factorybot.

I wrote a test that reproduced the behavior below but I wasn't quite sure what direction the PR should go in (fixing the auto corrector, or the rule)

The problem is a nested factory that looks like factory :active_article, traits: [:approved, :active] and:

  1. It gets identified as an error (should it be?)
  2. It gets corrected to association :factory, :active_article, traits: [:active, :approved] which breaks rubocop

The interesting thing is if I just add a do;end block to my :active_article factory, it ignores the error. I have a draft PR I'm going to push up shortly.

    context 'when implicit association has factory and traits' do
      it 'registers and corrects an offense' do
        expect_no_offenses(<<~RUBY)
          factory :article do
            traits :approved do
              approved { true }
            end

            traits :active do
              active { true }
            end

            factory :active_article, traits: [:approved, :active]
          end
        RUBY
      end
    end
jaydorsey commented 2 months ago

Some how, ended up on this repo instead of the rubocop-factory_bot one. Feel free to delete

pirj commented 2 months ago

This is fine, we’ve just recently extracted factory_bot-related cops, and the two gems are tightly bound until the next major release.