Skip to content

Ad hoc sequences in create_list #1647

@mike-burns

Description

@mike-burns

This comes up all the time. Here's an example from a real test:

prompts = [
  create(:prompt, name: "Prompt 1"),
  create(:prompt, name: "Prompt 2"),
  create(:prompt, name: "Prompt 3"),
]

I'd love to be able to write something else. Here's an example, maybe:

prompts = FactoryBot.create_list(
            :prompt,
            3,
            name: FactoryBot.build_sequence { |n| "Prompt #{n}" },
          )

Note that create_list already takes a block:

This block takes the produced object and the zero-based index as arguments, and returns an updated object.

But we want to get in there before it produces the object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions