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
RESTRICT_ON_SEND

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/factory_bot/create_list.rb, line 54
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, message: MSG_CREATE_LIST) do |corrector|
    CreateListCorrector.new(node.send_node).call(corrector)
  end
end
on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/factory_bot/create_list.rb, line 64
def on_send(node)
  return unless style == :n_times

  factory_list_call(node) do |count|
    message = format(MSG_N_TIMES, number: count)
    add_offense(node.loc.selector, message: message) do |corrector|
      TimesCorrector.new(node).call(corrector)
    end
  end
end

Private Instance Methods

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