From 0091c628e2a1fe87969ea70ef4907ac1aa0a96ce Mon Sep 17 00:00:00 2001 From: Jay Dorsey <191564+jaydorsey@users.noreply.github.com> Date: Wed, 24 Apr 2024 09:23:41 -0400 Subject: [PATCH] FactoryBot/AssociationStyle nested factories --- CHANGELOG.md | 3 +++ lib/rubocop/cop/factory_bot/association_style.rb | 1 + .../cop/factory_bot/association_style_spec.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 077d767a..68efc824 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Fix a false positive for `FactoryBot/AssociationStyle` when using nested factories with traits. ([@jaydorsey]) + ## 2.25.1 (2024-01-08) - Fix a false positive for `FactoryBot/CreateList` when create call does have method calls and repeat multiple times with other argument. ([@ydah]) @@ -88,6 +90,7 @@ [@ddieulivol]: https://github.com/ddieulivol [@dmitrytsepelev]: https://github.com/dmitrytsepelev [@harrylewis]: https://github.com/harrylewis +[@jaydorsey]: https://github.com/jaydorsey [@jfragoulis]: https://github.com/jfragoulis [@jonatas]: https://github.com/jonatas [@leoarnold]: https://github.com/leoarnold diff --git a/lib/rubocop/cop/factory_bot/association_style.rb b/lib/rubocop/cop/factory_bot/association_style.rb index 7a483e5a..4a08f46d 100644 --- a/lib/rubocop/cop/factory_bot/association_style.rb +++ b/lib/rubocop/cop/factory_bot/association_style.rb @@ -67,6 +67,7 @@ class AssociationStyle < ::RuboCop::Cop::Base # rubocop:disable Metrics/ClassLen DEFAULT_NON_IMPLICIT_ASSOCIATION_METHOD_NAMES = %w[ association + factory sequence skip_create traits_for_enum diff --git a/spec/rubocop/cop/factory_bot/association_style_spec.rb b/spec/rubocop/cop/factory_bot/association_style_spec.rb index 01afc883..874cfbd3 100644 --- a/spec/rubocop/cop/factory_bot/association_style_spec.rb +++ b/spec/rubocop/cop/factory_bot/association_style_spec.rb @@ -405,5 +405,19 @@ def inspected_source_filename RUBY end end + + context 'when factory inside a factory with inline traits' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + factory :order do + traits :completed do + completed_at { 3.days.ago } + end + + factory :order_completed, traits: [:completed] + end + RUBY + end + end end end