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

NameError on start of rspec spec with fresh Rails 8 app #511

Open
itsterry opened this issue Oct 31, 2024 · 1 comment
Open

NameError on start of rspec spec with fresh Rails 8 app #511

itsterry opened this issue Oct 31, 2024 · 1 comment
Labels

Comments

@itsterry
Copy link

itsterry commented Oct 31, 2024

Description

Running "bundle exec rspec spec" on a brand new Rails 8 app gives NameError: uninitialized constant FactoryBotRails::Railtie::FactoryBot

Reproduction Steps

gem install rails --pre # 8.0.0-rc2 at time of writing
rails g new myapp 

Edit Gemfile to look like this:

source "https://rubygems.org"

gem 'acts_as_list'
gem 'acts-as-taggable-on'
gem 'after_party'
gem 'ancestry'
gem 'aws-partitions'
gem 'aws-sdk-core'
gem 'aws-sdk-s3'

gem "bcrypt", "~> 3.1.7"
gem 'better_errors'
gem 'binding_of_caller'
gem "bootsnap", require: false
gem "brakeman", require: false, groups: %i[development test]

gem 'capistrano'
gem "cssbundling-rails"

gem "debug", platforms: %i[ mri windows ], require: "debug/prelude", groups: %i[development test]
gem 'decent_exposure'

gem 'factory_bot_rails', groups: %i[development test]
gem 'faker'
gem 'faraday_middleware-aws-sigv4'
gem 'friendly_id'

gem 'guard-rspec', groups: [:test]
gem 'guard-rubocop', groups: [:test]

gem "image_processing", "~> 1.2"

gem "jbuilder"
gem "jsbundling-rails"

gem "kamal", require: false

gem 'letter_opener', group: :development

gem 'morse_spec_helpers', groups: [:development]
gem "mysql2", "~> 0.5"

gem 'net-imap'
gem 'net-pop'
gem 'net-smtp'
gem 'net-ssh'

gem 'overcommit', groups: [:development]

gem "propshaft"
gem "puma", ">= 5.0"

gem 'rack-attack'
gem "rails", "~> 8.0.0.rc2"
gem 'ransack'
gem 'recaptcha'
gem 'rest-client'
gem 'rollbar'
gem 'rspec-rails', groups: %i[development test]
gem 'rubocop-rspec', groups: [:test]
gem "rubocop-rails-omakase", require: false, groups: %i[development test]

gem 'shoulda-matchers', groups: [:test]
gem 'simple_form'
gem 'simplecov', groups: [:test]
gem "solid_cable"
gem "solid_cache"
gem "solid_queue"
gem 'spring', groups: %i[developmen test]
gem 'spring-commands-rspec', groups: [:test]
gem "stimulus-rails"

gem "thruster", require: false
gem "turbo-rails"
gem "tzinfo-data", platforms: %i[ windows jruby ]

gem "web-console", group: :development 
gem 'whenever'
gem 'workflow-activerecord'

bundle install

rails g rspec:install

spec/rails_helper should look like this:
(I've removed comments for clarity)

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'factory_bot_rails'

begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  abort e.to_s.strip
end
RSpec.configure do |config|
  config.fixture_paths = [
    Rails.root.join('spec/fixtures')
  ]
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
end

bundle exec rspec spec

Expected behavior

The test suite runs without error (even though there are no tests yet)

Actual behavior

An error occurred while loading rails_helper.
Failure/Error: require_relative '../config/environment'

NameError:
  uninitialized constant FactoryBotRails::Railtie::FactoryBot
# /Users/terry/.rvm/gems/ruby-3.3.5/gems/factory_bot_rails-6.4.4/lib/factory_bot_rails/railtie.rb:12:in `<class:Railtie>'
# /Users/terry/.rvm/gems/ruby-3.3.5/gems/factory_bot_rails-6.4.4/lib/factory_bot_rails/railtie.rb:10:in `<module:FactoryBotRails>'
# /Users/terry/.rvm/gems/ruby-3.3.5/gems/factory_bot_rails-6.4.4/lib/factory_bot_rails/railtie.rb:9:in `<main>'
# /Users/terry/.rvm/gems/ruby-3.3.5/gems/bootsnap-1.18.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
# /Users/terry/.rvm/gems/ruby-3.3.5/gems/zeitwerk-2.7.1/lib/zeitwerk/core_ext/kernel.rb:34:in `require'
# /Users/terry/.rvm/gems/ruby-3.3.5/gems/factory_bot_rails-6.4.4/lib/factory_bot_rails.rb:1:in `<main>'
# /Users/terry/.rvm/gems/ruby-3.3.5/gems/zeitwerk-2.7.1/lib/zeitwerk/core_ext/kernel.rb:34:in `require'
# ./config/application.rb:19:in `<top (required)>'
# ./config/environment.rb:2:in `require_relative'
# ./config/environment.rb:2:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `require_relative'
# ./spec/rails_helper.rb:4:in `<top (required)>'

System configuration

factory_bot_rails version: 6.4.4
factory_bot version: 6.5.0
rails version: 8.0.0-rc2
ruby version: 3.3.5

Additional thoughts

This feels like it may be an order-of-loading issue, possibly down to the edge Rails version, but I figured it was worth raising it anyway

@itsterry itsterry added the bug label Oct 31, 2024
@itsterry
Copy link
Author

Observation: I can make this error go away if:

  1. I use the factory_bot gem rather than factory_bot_rails; AND
  2. I change any files that reference the FactoryBot class to have a filename that's alphabetically after rails_helper.rb

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

No branches or pull requests

1 participant