Factory Girl ArgumentError: Factory not registered:

I went through what seems like all the right steps, and I keep getting this error. From the gemfile:

group :test do gem 'rspec-rails' gem 'shoulda-matchers', require: false gem 'database_cleaner' gem 'factory_girl_rails', '~> 4.0', require: false gem 'faker' end 

spec_helper.rb:

 require 'factory_girl_rails' RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)] FactoryGirl.find_definitions FactoryGirl.factories.clear end 

specifications / factories / company.rb: require "spec_helper" requires "faker"

 FactoryGirl.define do factory :company do name { Faker::Name.name } system_name { Faker::Company.name } domain { Faker::Internet.url } end end 

company_spec.rb: require 'spec_helper'

 describe 'Company' do it 'has a valid factory' do FactoryGirl.build(:company).should be_valid end end 

Getting Error Error / Error:

 Failure/Error: FactoryGirl.create(:company).should be_valid ArgumentError: Factory not registered: company 
+4
source share
1 answer

I had everything, except for the most part, in the wrong place:

gemfile should be:

 group :development, :test do gem 'rspec-rails' gem 'shoulda-matchers', require: false gem 'database_cleaner' gem 'factory_girl_rails', '~> 4.0', require: false gem 'faker' end 

everything in spec_helper I switched to rails_helper.rb and got rid of two lines:

 require 'factory_girl_rails' RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)] // FactoryGirl.find_definitions // got rid of this // FactoryGirl.factories.clear // got rid of this end 

company_spec.rb:

 require 'rails_helper' describe 'Company' do it 'has a valid factory' do expect(build(:company)).to be_valid end end 

Most of this information was from comments and obtained from this tutorial about rspec and factory girl.

0
source

All Articles