Class: RuboCop::Cop::RSpec::FactoryBot::CreateList

Inherits:
Cop
  • Object
show all
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/rspec/factory_bot/create_list.rb

Overview

Checks for create_list usage.

This cop can be configured using the ‘EnforcedStyle` option

Examples:

‘EnforcedStyle: create_list`

# bad
3.times { create :user }

# good
create_list :user, 3

# good
3.times { |n| create :user, created_at: n.months.ago }

‘EnforcedStyle: n_times`

# bad
create_list :user, 3

# good
3.times { create :user }

Defined Under Namespace

Classes: Corrector, CreateListCorrector, TimesCorrector

Constant Summary collapse

MSG_CREATE_LIST =
'Prefer create_list.'
MSG_N_TIMES =
'Prefer %<number>s.times.'

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rubocop/cop/rspec/factory_bot/create_list.rb', line 70

def autocorrect(node)
  if style == :create_list
    CreateListCorrector.new(node)
  else
    TimesCorrector.new(node)
  end
end

#on_block(node) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/rspec/factory_bot/create_list.rb', line 49

def on_block(node)
  return unless style == :create_list
  return unless n_times_block_without_arg?(node)
  return unless contains_only_factory?(node.body)

  add_offense(node.send_node,
              location: :expression, message: MSG_CREATE_LIST)
end

#on_send(node) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/cop/rspec/factory_bot/create_list.rb', line 58

def on_send(node)
  return unless style == :n_times

  factory_list_call(node) do |_receiver, _factory, count, _|
    add_offense(
      node,
      location: :selector,
      message: format(MSG_N_TIMES, number: count)
    )
  end
end