thoughtbot / thoughtbot/factory_bot

Ad hoc sequences in create_list

Open
#1,647 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Ruby
Stars
8.2k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at create_list and trace how attributes are evaluated before the object is produced; compare the proposed build_sequence usage with the existing create_list block behavior. Done means create_list supports an ad hoc sequence for attributes while preserving its current block semantics, including the example shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.