Factory_bot: Child factories with explicit classes still try to refer to the parent's class.

Created on 26 Jan 2021  路  5Comments  路  Source: thoughtbot/factory_bot

Description

Prior to 6.0.0, one could have a factory with no associated class so long as the child factory specified a class. 6.0.0 and up try to reference the non-existent parent class despite the explicit child class.

We've fixed this by changing the factory to a trait, as it probably should be, but it is an undocumented regression and complicated our FactoryBot upgrade.

Reproduction Steps

require 'factory_bot'

p Gem.loaded_specs["factory_bot"].version

class This
  class Exists
    def test
      "ok"
    end
  end
end

FactoryBot.define do
  factory :does_not_exist do
  end

  factory :this_exists, class: "This::Exists", parent: :does_not_exist do
  end
end

p FactoryBot.build(:this_exists).test

Expected behavior

It uses the explicit child class. It prints the Gem version and "ok"

Actual behavior

It tries to reference the class implied by the parent factory.

/Users/schwern/.rvm/gems/ruby-2.6.5/gems/activesupport-6.1.1/lib/active_support/inflector/methods.rb:274:in `constantize': uninitialized constant DoesNotExist (NameError)

System configuration

factory_bot version: 6.1.1
rails version: N/A
ruby version: 2.6.5

bug

All 5 comments

Glancing quickly, I think this is a duplicate of https://github.com/thoughtbot/factory_bot/issues/1409

@composerinteralia Yes, it is, and I agree it's not needed.

However, it isn't documented. Is there a place I can document its removal in 6.0.0, and the fix? Maybe the 6.0.0 tag can be bumped?

How about adding this to the 6.0.0 release?

  • Removed: support for "abstract" factories with no associated class; use traits instead.

Would you be willing to open a PR adding this to the NEWS.md file for the 6.0.0 release? Then I can update the relevant release notes to match.

We've merged #1463, which addresses this issue. Thank you @schwern! Closing.

Was this page helpful?
0 / 5 - 0 ratings