Ruby Gem: Uninitialized FactoryBot constant

Working with a Ruby stone and trying to use FactoryBot internally 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 worked fine when I used the old factory_girl stone, but it broke with renaming to factory_bot. Thanks!!

+6
source share
2 answers

Doh. Stupid mistake here by running bundle exec rake specinstead rake spec.

It was also necessary to add require 'factory_bot'to the beginningsupport/factory_bot.rb

+9
source

Review just in case you do it from scratch

rspec ( gem Gemfile, bundle install)

rspec rails rails g rspec:install

spec/support/factory_bot.rb, :

require 'factory_bot'

RSpec.configure do |config|
    config.include FactoryBot::Syntax::Methods
end

# RSpec without Rails
RSpec.configure do |config|
    config.include FactoryBot::Syntax::Methods

    config.before(:suite) do
        FactoryBot.find_definitions
    end
end

spec/rails_helper.rb

require 'support/factory_bot'

fixture ,   config.use_transactional_fixtures = true

!, , spec , rspec .: spec/features/my_action_spec.rb

spec/models/my_model_spec.rb

spec/task/my_task_spec.rb

rspec

rails rspec

bundle exec rspec

, - rspec + FactoryBot gem

+1

All Articles