Skip to content

Commit

Permalink
Fix a false positive for FactoryBot/CreateList (#97)
Browse files Browse the repository at this point in the history
Fix: #87
  • Loading branch information
ydah authored Jan 5, 2024
1 parent e0c0e5d commit 85bae8d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix a false positive for `FactoryBot/CreateList` when create call does have method calls and repeat multiple times with other argument. ([@ydah])

## 2.25.0 (2024-01-04)

- Fix a false positive for `FactoryBot/FactoryNameStyle` when namespaced models. ([@ydah])
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/factory_bot/create_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CreateList < ::RuboCop::Cop::Base # rubocop:disable Metrics/ClassLength

# @!method arguments_include_method_call?(node)
def_node_matcher :arguments_include_method_call?, <<~PATTERN
(send #factory_call? :create sym `(send ...))
(send #factory_call? :create sym ... `(send ...))
PATTERN

# @!method factory_call(node)
Expand Down
18 changes: 7 additions & 11 deletions spec/rubocop/cop/factory_bot/create_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
RUBY
end

it 'ignores n.times when create call does have method calls ' \
'and repeat multiple times with other argument' do
expect_no_offenses(<<~RUBY)
3.times { |n| create :user, :admin, repositories_count: rand }
RUBY
end

it 'ignores n.times when create call does have method calls ' \
'and not repeat' do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -138,17 +145,6 @@
RUBY
end

it 'flags usage of n.times with keyword arguments' do
expect_offense(<<~RUBY)
5.times { create :user, :trait, key: val }
^^^^^^^ Prefer create_list.
RUBY

expect_correction(<<~RUBY)
create_list :user, 5, :trait, key: val
RUBY
end

it 'flags usage of n.times with block argument' do
expect_offense(<<~RUBY)
3.times do
Expand Down

0 comments on commit 85bae8d

Please sign in to comment.