Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent factory with traits that call other traits uses trait from child factory #1716

Open
EmilioCristalli opened this issue Nov 8, 2024 · 5 comments · May be fixed by #1717
Open

Parent factory with traits that call other traits uses trait from child factory #1716

EmilioCristalli opened this issue Nov 8, 2024 · 5 comments · May be fixed by #1717
Labels

Comments

@EmilioCristalli
Copy link

Description

Not sure if this is a bug or expected behavior, but it felt confusing to me.

I have a factory A with a trait trait_a that calls another trait trait_b.
And I have another factory B that inherits from A and overrides trait_b.

If I first use factory B, then factory A starts using trait_b from B when trait_a is called (probably more clear with the example below haha)

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 :title
    t.string :content
  end
end

class Post < ActiveRecord::Base
end

FactoryBot.define do
  factory :post do
    trait :with_title_and_content do
      with_title
      with_content
    end

    trait :with_title do
      title { "generic title" }
    end

    trait :with_content do
      content { "generic content" }
    end
  end

  factory :tech_post, parent: :post do
    trait :with_content do
      content { "tech content" }
    end
  end
end

class FactoryBotTest < Minitest::Test
  def test_factory_bot_stuff
    tech_post = FactoryBot.build(:tech_post, :with_title_and_content)
    assert_equal "generic title", tech_post.title
    assert_equal "tech content", tech_post.content

    generic_post = FactoryBot.build(:post, :with_title_and_content)
    assert_equal "generic title", generic_post.title
    assert_equal "generic content", generic_post.content
  end
end

# Run the tests with `ruby <filename>`

Expected behavior

The test should pass, because the parent factory should use the with_content trait from the parent factory

Actual behavior

It fails, because it uses the with_content trait from the child tech_post factory

FactoryBotTest#test_factory_bot_stuff [factory.rb:61]:
Expected: "generic content"
  Actual: "tech content"

System configuration

factory_bot version: 6.5.0
rails version: 7.2.1
ruby version: 3.3.5

@CodeMeister
Copy link

Hi @EmilioCristalli,

Love the reproduction steps ... made it much easier 👍

I've submitted a pull request that should fix this issue #1717

Have a look and see if it works for you?

As it's relatively small, I'd love it if you could create a new branch, merge the request (or cut/paste the changes) and run your tests. Real world tests are a far better indication of success. 🫶

@EmilioCristalli
Copy link
Author

thanks for looking into this @CodeMeister!!

your PR seems to fix the issue for me

@CodeMeister
Copy link

Hi @EmilioCristalli. It would be best to leave this issue open until the PR is merged. Otherwise, the team may think it was fixed by another solution.

🎉

@EmilioCristalli
Copy link
Author

lol i mentioned your PR fixes this issue in a PR to use a workaround in a private repo, and github automatically closed this issue when i merged it haha

sorry about that, will reopen it

@CodeMeister
Copy link

Hehe, GitHub works in mysterious ways...

Thanks for re-opening 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants