class RuboCop::Cop::RSpec::FactoryBot::CreateList

Checks for create_list usage.

This cop can be configured using the `EnforcedStyle` option

@example `EnforcedStyle: create_list`

# bad
3.times { create :user }

# good
create_list :user, 3

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

@example `EnforcedStyle: n_times`

# bad
create_list :user, 3

# good
3.times { create :user }

Constants

MSG_CREATE_LIST
MSG_N_TIMES

Public Instance Methods

autocorrect(node) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

contains_only_factory?(node) click to toggle source
# File lib/rubocop/cop/rspec/factory_bot/create_list.rb, line 80
def contains_only_factory?(node)
  if node.block_type?
    factory_call(node.send_node)
  else
    factory_call(node)
  end
end