thoughtbot / thoughtbot/factory_bot

Inconsistent and brittle trait inheritance behavior

Open
#968 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Given this this class and this factory definition:

DataClass = Struct.new(:x, :y, :z)

FactoryGirl.define do
  factory :parent_factory, class: DataClass do
    x 1

    trait :y do
      y 1
    end

    trait :z do
      y
      z 1
    end
  end

  factory :child_factory, class: DataClass, parent: :parent_factory do
    x 2

    trait :y do
      y 2
    end
  end
end

You observe this behaviour:

require 'factory_girl'
FactoryGirl::VERSION
# => '4.7.0'

FactoryGirl.find_definitions
FactoryGirl.build(:parent_factory, :z)
# => <struct DataClass x=1, y=1, z=1>
FactoryGirl.build(:child_factory, :z)
# => <struct DataClass x=2, y=1, z=1>

FactoryGirl.reload
FactoryGirl.build(:child_factory, :z)
# => <struct DataClass x=2, y=2, z=1>
FactoryGirl.build(:parent_factory, :z)
# => <struct DataClass x=1, y=2, z=1>

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

Reproduce the behavior through FactoryGirl.find_definitions, FactoryGirl.build, and FactoryGirl.reload using the factory and trait definitions in the report. Trace how parent and child traits are resolved before and after reload. Done means trait inheritance produces consistent results regardless of build order or whether FactoryGirl.reload was called.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.