Steven
Steven

Reputation: 1258

RSpec factories with FactoryBot 'uninitialized constant FactoryBot'

I am setting up request specs following this tutorial. If I run the tests with bundle exec rspec I get the following error:

An error occurred while loading ./spec/factories/entities_spec.rb.
Failure/Error:
  FactoryBot.define do
    factory :entity do
      name { FFaker::Lorem.word }
    end
  end

NameError:
  uninitialized constant FactoryBot

The tutorial only asks to add some configuration to the rails_helper.rb file. I can't figure where I need to add a require 'factory_bot'. If I add it to the top of the rails_helper.rb file, the error persists. And if I add it to the factories/entity_spec.rb file, I get this error:

An error occurred while loading ./spec/requests/entities_spec.rb.
Failure/Error:
  factory :entity do
    name { FFaker::Lorem.word }
  end

FactoryBot::DuplicateDefinitionError:
  Factory already registered: entity

Upvotes: 1

Views: 3990

Answers (1)

Steven
Steven

Reputation: 1258

Simply renaming my factories/*.rb files from e.g. factories/user_spec.rb to factories/user.rb solved the problem.

Upvotes: 1

Related Questions