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/AssociationStyle autocorrection is wrong when the trait is not a symbol literal #105

Open
r7kamura opened this issue Feb 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@r7kamura
Copy link
Contributor

r7kamura commented Feb 20, 2024

Ideally, I expect the following to be autocorrection:

# bad
association :user, foo

# good
user factory: [:user, foo]

However, when the trait is such a non-symbol-literal, the autocorrection unintentionally removes it like this:

# autocorrection result
user

This behavior can be confirmed by the following test case:

diff --git a/spec/rubocop/cop/factory_bot/association_style_spec.rb b/spec/rubocop/cop/factory_bot/association_style_spec.rb
index 01afc88..fbe0bab 100644
--- a/spec/rubocop/cop/factory_bot/association_style_spec.rb
+++ b/spec/rubocop/cop/factory_bot/association_style_spec.rb
@@ -113,6 +113,23 @@ RSpec.describe RuboCop::Cop::FactoryBot::AssociationStyle do
       end
     end
 
+    context 'when `association` is called with non-symbol trait' do
+      it 'registers and corrects an offense' do
+        expect_offense(<<~RUBY)
+          factory :article do
+            association :user, foo
+            ^^^^^^^^^^^^^^^^^^^^^^ Use implicit style to define associations.
+          end
+        RUBY
+
+        expect_correction(<<~RUBY)
+          factory :article do
+            user factory: [:user, foo]
+          end
+        RUBY
+      end
+    end
+
     context 'when `association` is called with factory option' do
       it 'registers and corrects an offense' do
         expect_offense(<<~RUBY)
Failures:

  1) RuboCop::Cop::FactoryBot::AssociationStyle when EnforcedStyle is :implicit when `association` is called with non-symbol trait registers and corrects an offense
     Failure/Error:
       expect_correction(<<~RUBY)
         factory :article do
           user factory: [:user, foo]
         end
       RUBY
     
       expected: "factory :article do\n  user factory: [:user, foo]\nend\n"
            got: "factory :article do\n  user\nend\n"
     
       (compared using ==)
     
       Diff:
       @@ -1,4 +1,4 @@
        factory :article do
       -  user factory: [:user, foo]
       +  user
        end
       
     # ./spec/rubocop/cop/factory_bot/association_style_spec.rb:125:in `block (4 levels) in <top (required)>'
@ydah ydah added the bug Something isn't working label Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants