thoughtbot/factory_bot · error · ArgumentError

count missing for #{strategy_name}_list

Error message

count missing for #{strategy_name}_list

What it means

ArgumentError raised by the *_list strategy methods (strategy_syntax_method_registrar.rb:35-38): create_list, build_list, build_stubbed_list, and attributes_for_list require an integer count as the second positional argument. The guard `amount.respond_to?(:times)` fires when that slot holds anything else — usually the count was omitted and the overrides hash or first trait slid into the position.

Source

Thrown at lib/factory_bot/strategy_syntax_method_registrar.rb:37

      end
    end

    private

    def define_singular_strategy_method
      strategy_name = @strategy_name

      define_syntax_method(strategy_name) do |name, *traits_and_overrides, &block|
        FactoryRunner.new(name, strategy_name, traits_and_overrides).run(&block)
      end
    end

    def define_list_strategy_method
      strategy_name = @strategy_name

      define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides, &block|
        unless amount.respond_to?(:times)
          raise ArgumentError, "count missing for #{strategy_name}_list"
        end

        Array.new(amount) do |i|
          block_with_index = StrategySyntaxMethodRegistrar.with_index(block, i)
          send(strategy_name, name, *traits_and_overrides, &block_with_index)
        end
      end
    end

    def define_pair_strategy_method
      strategy_name = @strategy_name

      define_syntax_method("#{strategy_name}_pair") do |name, *traits_and_overrides, &block|
        Array.new(2) { send(strategy_name, name, *traits_and_overrides, &block) }
      end
    end

    def define_syntax_method(name, &block)

View on GitHub (pinned to 18ae8b581b)

Solutions

  1. Insert an integer count as the second argument: `create_list(:post, 3, title: 'The Hunting of the Bear')`.
  2. Keep traits and overrides after the count: `build_list(:user, 5, :admin, name: 'X')`.
  3. Use *_pair when exactly two records are needed — it takes no count.
  4. When forwarding dynamic counts from params, coerce first: `Integer(params[:count])`.

Example fix

# before
FactoryBot.create_list(:post, title: 'The Hunting of the Bear') # hash lands in count slot

# after
FactoryBot.create_list(:post, 3, title: 'The Hunting of the Bear')
Defensive patterns

Strategy: validation

Validate before calling

# coerce user-supplied counts before calling *_list
count = Integer(params[:count]) rescue nil
raise ArgumentError, 'count must be a positive integer' unless count&.positive?
FactoryBot.create_list(:post, count, title: 'X')

Type guard

->(n) { n.is_a?(Integer) && n >= 0 }

Prevention

When it happens

Trigger: `FactoryBot.create_list(:post, title: 'The Hunting of the Bear')` (count forgotten, the hash becomes amount); `build_list(:user, :admin)` (trait lands in the count slot); `create_list(:post, '3')` (string count). Each raises 'count missing for create_list' / '..._list' for the strategy used.

Common situations: Editing an existing *_list call and accidentally deleting the count; adding traits or overrides and forgetting the count must remain the second argument; find/replace refactors that shift positional args.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of thoughtbot/factory_bot@18ae8b581b (2026-08-21). Data as JSON: /api/errors/545ed2b01c096968. Report an issue: GitHub.