Class: RuboCop::Cop::RSpec::CreateListMax

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

Overview

Prevent creating to most records with ‘FactoryBot.create_list`. Creating to much records can significantly increase spec time.

Examples:

Max: 5 (default)

# Maximum amount params allowed for `create_list`.

# bad
create_list :my_model, 10

# good
create_list :my_model, 5

# good
build_stubbed_list :my_model, 10

Constant Summary collapse

MSG =
'Creating more than `%<max_config>s` records is discouraged.'
DEFAULT_MAX =
5
RESTRICT_ON_SEND =
[:create_list].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/rspec/create_list_max.rb', line 30

def on_send(node)
  amount = create_list(node).to_a.first

  return unless amount

  max_config = cop_config['Max'] || DEFAULT_MAX

  add_offense(node, message: format(MSG, max_config: max_config)) if amount > max_config
end