thoughtbot / factory_bot_rails

Factory Bot ♥ Rails

Home Page:https://thoughtbot.com/services/ruby-on-rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting FactoryBot not registered for some factories while others are working

aditya01933 opened this issue · comments

In my factories, this one is working:

spec/factories/cities.rb

FactoryBot.define do
  factory :city do
    name { Faker::Address.city }
    state { create(:state) }
  end
end

while this one is not working:
/spec/factories/admins.rb

FactoryBot.define do
  factory :admin do
    sequence(:email) { |n| "email-#{n}@example.com" }
    phone { rand(10 ** 10) }
    password '12345678'
    avatar { create(:image) }
    first_name "first_name"
    last_name "last_name"
  end
end

I am not able to understand why this could happen, please help me solve/debug the problem.

I am on the latest version of the gem.

These factories are working when I am explicitly requiring them.

Which test framework are you using? Can you give more information such as spec_helper?

Yes, I am using rspec and it's working when I am requiring the files in spec_helper

+1

Experiencing the exact same problem, I'm also using RSpec. My User factory works,

# spec/factories/user.rb

FactoryBot.define do
  factory :user do
    email { Faker::Internet.email }
    password 'password'
    password_confirmation 'password'
  end
end

while my Link factory does not.

# spec/factories/link.rb

FactoryBot.define do
  factory :link do
    address Faker::ParksAndRec.city
    link Faker::Internet.slug
  end
end

Tried with create :link as well as create_list :link, none works. Below is a gist of my RSpec config, with part related to my test configuration.

https://gist.github.com/taufnrsyd/0f56478dc8463103694f8115e23e20a9

Found the problem!! In my spec_helper. I was requiring factory_bot, changing it to require factory_bot_rails worked for me.

This should be documented in the project Readme.

Found the problem!! In my spec_helper. I was requiring factory_bot, changing it to require factory_bot_rails worked for me.