thoughtbot / thoughtbot/factory_bot

Ability to checkpoint and restore sequence state

Open
#1,631 0 comments 0 reactions 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

Problem this feature will solve

Several of our tests rely on sequences being reset between examples. So we call FactoryBot.reload in a before(:each). But this interferes with us being able to use create in a before(:all). For example:

RSpec.describe '…' do
  before(:all) { @user = create(:user) }

  before(:each) { FactoryBot.reload }

  it '…' { another_user = create(:user) }
end

The second call to create will attempt to reuse the same ID from the first call.

Desired solution

It might be good if we could somehow checkpoint the state of the sequences, then restore to that state before each example:

RSpec.describe '…' do
  before(:all) {
    FactoryBot.reload
    @user = create(:user)
    FactoryBot.snapshot
  }

  before(:each) { FactoryBot.rollback }

  # …
end

Alternatives considered

Additional context

We rely on IDs being reset between examples because we use approval tests, and in some cases, object IDs appear in the approval output, and it's not straightforward for us to elide them. So we depend on IDs being generated predictably for each example.

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 by tracing the FactoryBot.reload entry point and how it resets sequences. Define how FactoryBot.snapshot and FactoryBot.rollback should preserve and restore sequence state, then verify that IDs remain predictable across the before(:all), before(:each), and create calls shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.