Class: Rubocop::Cop::RSpec::FactoryBot::ExcessiveCreateList

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/factory_bot/excessive_create_list.rb

Overview

Check for create_list FactoryBot declarations higher than configured MaxAmount.

Examples:

MaxAmount: 20

We do not allow more than 20 items to be created.

# bad
create_list(:merge_request, 1000, state: :opened)

# good
create_list(:merge_request, 15, state: :opened)
We do not allow more than 10 items to be created (default)
# bad
create_list(:merge_request, 1000, state: :opened)

# good
create_list(:merge_request, 10, state: :opened)

Constant Summary collapse

MESSAGE =
'Avoid using `create_list` with more than %{max_amount} items.'
RESTRICT_ON_SEND =
i[create_list].freeze

Instance Method Summary collapse

Instance Method Details

#create_list?(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/rspec/factory_bot/excessive_create_list.rb', line 33

def_node_matcher :create_list?, "(send nil? :create_list (sym ...) $(int _) ...)\n"

#on_send(node) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/rspec/factory_bot/excessive_create_list.rb', line 39

def on_send(node)
  number_node = create_list?(node)
  return unless number_node

  max_amount = cop_config['MaxAmount']
  return if number_node.value <= max_amount

  add_offense(number_node, message: format(MESSAGE, max_amount: max_amount))
end