27
votes

I am trying to get factory girl to run with rspec in my rails 4.1.1 app.

Problem is when I run rspec in my command line, i get Failure/Error: verse = build(:verse) ArgumentError: Factory not registered: verse.

I am at loss because I checked the factory girl getting started page and many answers here on SO andI still can't fix this issue.

in my Gemfile:

gem 'rails', '4.1.1'
group :development, :test do
  gem 'rspec-rails'
  gem "factory_girl_rails"
end

my spec_helper.rb file:

require 'factory_girl_rails'
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

spec/controllers/api/verses_controller_spec.rb

describe "API Controller" do
  describe "show a verse" do
    it "should return status 200" do
      verse = build(:verse)
      get :show, id: verse.id
      expect(response).to have_http_status(200)
    end
    it "should return json object" do
      verse = build(:verse)
      get :show, id: verse.id
      JSON.parse(response.body).should == {'id' => verse.id}
    end
  end
end

spec/factories/verses.rb

FactoryGirl.define do
  factory :verse do
    line1 "A beautiful verse I stand"
  end
end

Why isn't my factory loading properly? Files in the spec/factories folder are supposed to get loaded automatically.

3
it looks like everything is correct. Do you use spork or spring - if yes try to reload it. - gotva
I just noticed Rails 4.1 uses spring by default. I am new to spring, I tried stop and start but that didn't solve the issue. - fkoessler
let's try to load rails c and execute FactoryGirl.build(:verse). Do you have other factories? Try any of them too. Does any factory work? - gotva
This part of the issue seems to be fixed thanks to your suggestion and this post: github.com/rails/spring/issues/88. I added config.before(:all) do FactoryGirl.reload end lines in my spec_helper.rb I am now getting Failure/Error: verse = build(:verse) NameError: uninitialized constant Verse - fkoessler
add this as an answer because the problem is really weird and it will help others - gotva

3 Answers

51
votes

There seems to be an issue when using rspec / factory girl with spring.

Adding:

config.before(:all) do
  FactoryGirl.reload
end

in my spec_helper.rb solved the issue.

Credit: https://github.com/rails/spring/issues/88

Edit:

Another way to fix the issue is to manually tell Factory Girl where to load the factory. Add this in your spec_helper:

FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions
14
votes

This seems to be an issue with Factory Bot. I fixed it (as per the issue report) with FactoryBot.find_definitions:

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

  config.before do
    FactoryBot.find_definitions
  end
end
5
votes

This is not necessarily caused by Spring. There is an issue that means factory_girl loads the paths slightly different from rspec. The solution is to to add to the rails_helper the following

FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
FactoryGirl.find_definitions

assuming the helper is at engine_root/spec.

This occurs when you are using rspec in a rails engine.