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

Fix a false positive for FactoryBot/CreateList #97

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

ydah marked this conversation as resolved.
Show resolved Hide resolved
it 'flags usage of n.times with block argument' do
expect_offense(<<~RUBY)
3.times do
Expand Down