Skip to content

Traits that imply other ones can cause double running of callbacks #1703

Open
@saty9

Description

@saty9

Description

When i make a trait B that implies A and build with B and A traits the after(:build) in A runs twice

Reproduction Steps

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
  gem "factory_bot", "~> 6.0"
  gem "activerecord"
  gem "sqlite3"
end

require "active_record"
require "factory_bot"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.string :body
  end
end

# TODO: Add any application specific code necessary to reproduce the bug
class Post < ActiveRecord::Base
end

FactoryBot.define do
  factory :post do
    body { "Post body" }

    trait :implies_foo do
      foo
    end

    trait :foo do
      after(:build) do |post|
        post.body += " with foo"
      end
    end
  end
end

class FactoryBotTest < Minitest::Test
  def test_factory_bot_stuff
    post = FactoryBot.build(:post, :foo, :implies_foo)

    assert_equal post.body, "Post body with foo"
  end
end

# Run the tests with `ruby <filename>`

Expected behavior

I expect the after(:build) block to be run once

Actual behavior

It runs twice instead

System configuration

factory_bot version: 6.4.5
rails version: 7.2.1
ruby version: 3.3.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions