Factory not registered

I had run into the following issue with the structure in my folder factories/ - factories/ -- artists.rb -- techniques.rb

artists.rb

FactoryBot.define do
    factory :artist do
      name       'Michael'
      technique   FactoryBot.create :technique
    end
  end

techniques.rb

FactoryBot.define do
  factory :technique do
    name 'Some name'
  end
end

So it was loading artist before technique object were loaded. So it couldn't find it. The solution is to not use nested FactoryBot create in factories or rename your nested factories to something that stands before your parent factory.

I just moved my technique factory. to factories.rb and defined it there. And issue was resolved.


You have your factory definition in the wrong file, according to your question it is in user.rb. This needs to be in a factories.rb in your test folder (spec) if you use rspec

# user.rb

FactoryGirl.define do
    factory :user do |f|
        f.email "[email protected]"
        f.password "ruby"
        f.password_confrimation "ruby"
    end
end

Change above to this, (Also you don't need the f variable)

# spec/factories.rb

FactoryGirl.define do
    factory :user do
        email "[email protected]"
        password "ruby"
        password_confrimation "ruby"
    end
end

Also as the comments say, make sure gem 'factory_girl_rails' is in your Gemfile, instead of just gem 'factory_girl'