Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Gem: Uninitialized constant FactoryBot

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!!

like image 513
Bryan L. Avatar asked Jan 04 '18 08:01

Bryan L.


People also ask

What does uninitialized constant mean in Ruby?

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.

What is the factorybot module?

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.

What is a Factory_Bot string?

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.

What should I look for when using Factory_Bot?

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.


1 Answers

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

like image 72
Bryan L. Avatar answered Sep 22 '22 14:09

Bryan L.