thoughtbot / factory_bot

A library for setting up Ruby objects as test data.
https://thoughtbot.com
MIT License
7.91k stars 2.6k forks source link

Factory Bot does not work with the latest activesupport release #1690

Open Schmitze333 opened 3 weeks ago

Schmitze333 commented 3 weeks ago

Description

In combination with the latest activesupport gem (version 7.2.1) just requiring factory_bot resolves in the following error:

.../lib/ruby/gems/3.3.0/gems/activesupport-7.2.1/lib/active_support/delegation.rb:47:in `generate': uninitialized constant #<Class:ActiveSupport::Delegation>::Inflector (NameError)

          unless Inflector.safe_constantize(to.name).equal?(to)
                 ^^^^^^^^^
    from .../lib/ruby/gems/3.3.0/gems/activesupport-7.2.1/lib/active_support/core_ext/module/delegation.rb:161:in `delegate'
    from .../lib/ruby/gems/3.3.0/gems/factory_bot-6.4.6/lib/factory_bot/definition_hierarchy.rb:3:in `<class:DefinitionHierarchy>'
    from.../lib/ruby/gems/3.3.0/gems/factory_bot-6.4.6/lib/factory_bot/definition_hierarchy.rb:2:in `<module:FactoryBot>'
    from.../lib/ruby/gems/3.3.0/gems/factory_bot-6.4.6/lib/factory_bot/definition_hierarchy.rb:1:in `<top (required)>'
    from <internal: .../lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require'
    from <internal: ...lib//ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require'
    from .../lib/ruby/gems/3.3.0/gems/factory_bot-6.4.6/lib/factory_bot.rb:8:in `<top (required)>'
    from <internal: .../lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:141:in `require'
    from <internal: .../lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:141:in `rescue in require'
    from <internal: .../lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:135:in `require'
    from test.rb:1:in `<main>'
<internal: .../ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require': cannot load such file -- factory_bot (LoadError)
    from <internal: .../lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require'
    from test.rb:1:in `<main>'

Reproduction Steps

Gemfile:

# frozen_string_literal: true

source 'https://rubygems.org'

gem 'factory_bot'

Test file:

# test.rb
require 'factory_bot'

Running: ruby test.rb will result in the above error.

But also a typical setup with requiring factory_bot in the spec_helper will fail.

Expected behavior

factory_bot should be able to load.

Actual behavior

factory_bot crashes.

System configuration

factory_bot version: 6.4.6 rails version:
ruby version: tested with 3.3.1 and 3.3.4

Schmitze333 commented 3 weeks ago

Adding active_support/inflector to the requires in factory_bot.rb solves the problem:

require "set"
require "active_support/core_ext/module/delegation"
require "active_support/core_ext/module/attribute_accessors"
require "active_support/deprecation"
require "active_support/notifications"
require "active_support/inflector"

require "factory_bot/internal"
require "factory_bot/definition_hierarchy"
require "factory_bot/configuration"
require "factory_bot/errors"
...

But it's kind of strange that one has to add a class that is used by activesupport internally.

souadfettah commented 3 weeks ago

Can you add to your gemfile this gem 'activesupport', '< 7.2.1'

Then run: bundle update activesupport

Add the following to the top of your test.rb or spec_helper.rb file: require 'active_support/inflector' require 'factory_bot'

Schmitze333 commented 3 weeks ago

@souadfettah Thank you for the very fast reply ❤️ !

I know how to hot-fix the problem in my projects. Nevertheless, I think that should be fixed in factory_bot.