Working on a Ruby gem and trying to use FactoryBot inside with RSpec.
I have this in support/factory_bot.rb
:
RSpec.configure do |config| config.include FactoryBot::Syntax::Methods config.before(:suite) do FactoryBot.find_definitions end end
and in spec_helper.rb
:
require 'support/factory_bot'
When I try to run the spec rake task, I get this error:
support/factory_bot.rb:2:in `block in <top (required)>': uninitialized constant FactoryBot (NameError)
What am I missing? This used to work fine when I was using the old factory_girl gem, but it has broken with the rename to factory_bot. Thanks!!
Ruby NameError Uninitialized Constant Causes. The Uninitialized Constant error is a variation of a regular NameError exception class. It has several possible causes. You'll see this error when the code refers to a class or module that it can't find, often because the code doesn't include require, which instructs the Ruby file to load the class.
This module is a container for all strategy methods provided by FactoryBot. This includes all the default strategies provided ( #build, #create, #build_stubbed, and #attributes_for ), as well as the complementary *_list and *_pair methods. (see #strategy_method) Generates a hash of attributes for a registered factory by name.
An Array of strings specifying locations that should be searched for factory definitions. By default, factory_bot will attempt to require “factories”, “test/factories” and “spec/factories”. Only the first existing file will be loaded.
By default, factory_bot will attempt to require “factories”, “test/factories” and “spec/factories”. Only the first existing file will be loaded. starting_id Integer The new starting id value. Look for errors in factories and (optionally) their traits.
Doh. Silly mistake here, running bundle exec rake spec
instead of rake spec
solved it.
Also had to add require 'factory_bot'
to the top of support/factory_bot.rb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With