thoughtbot / thoughtbot/factory_bot

`modify` doesn't allow changing the class

Open
#1,455 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

FactoryBot.modify doesn't allow changing a factory's class. It's not explicitly mentioned in the docs, so this could reasonably be classified as a feature request. Though I rather expected it to be possible.

Reproduction Steps

Given:

class Test; end

class TestNew; end

FactoryBot.define do
  factory :test do; end
end

FactoryBot.modify do
  factory :test, class: "TestNew" do; end
end
Expected behavior

I expected FactoryBot.build :test to return an instance of TestNew.

Actual behavior
[2] pry(main)> FactoryBot.build :test
=> #<Test:0x00007fe87d6c1ed0>
System configuration

factory_bot version: 5.1.1
rails version: 6.0
ruby version: 2.6.5

Rationale

I'm sure this probably seems like an odd thing to do. Here's the scenario: We are migrating models from a legacy database, so we have the legacy db models defined is Legacy::MyRecord where each (or most) of the legacy models has a corresponding MyRecord (no-namespace) which lives in the new database. The application code (including the existing factories) all reference Legacy::*.

We are using a feature flag to allow the code to run where each Legacy::* constant is instead redefined to be the new class. (We're using class Legacy::Foo < Foo to keep AR happy.) However, factory bot (and active record's) introspection doesn't allow the constant redefinition to "just work". For the AR associations to work, the factory bot factory needs to be of the right class.

There are dozens of legacy models to be migrated, which means dozens of factories that we'd prefer not to duplicate. The hope was that we could use .modify to change each factory's class when the feature flag is enabled. But this appears to have no effect.

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 with the reported FactoryBot.modify example and inspect the FactoryBot.modify entry point to determine why the factory class remains unchanged. Reproduce the behavior against the stated Ruby and Rails versions; done means FactoryBot.build :test returns an instance of TestNew when the modified class is supplied.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
testing
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.