rubocop-factory_bot
rubocop-factory_bot copied to clipboard
`FactoryBot/AssociationStyle` autocorrection is wrong when the trait is not a symbol literal
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)>'