peterhurford peterhurford - 1 year ago 194
Ruby Question

ArgumentError: Factory not registered

I'm trying to run rspec with FactoryGirl, but I keep getting this error:

1) Products Update with invalid information
Failure/Error: let(:product) { FactoryGirl.create(:product) }
Factory not registered: product
# ./spec/requests/products_spec.rb:47:in `block (3 levels) in <top (required)>'
# ./spec/requests/products_spec.rb:52:in `block (3 levels) in <top (required)>'


Here's the test with the error (spec/requests/products_spec.rb):

describe "Read" do
let(:product) { FactoryGirl.create(:product) }
before { visit product_path(product) }
it { should have_text(product.title) }


Here's the factory (spec/factories.rb):

FactoryGirl.define do
factory :product do
title "Lorem ipsum"
description "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vitae ipsum consectetur, semper dolor sed, dignissim enim."
image_url "lorem.png"
price 9.95


I restarted the server (with Spork and Guard) and nothing changed, though I may not be restarting Spork/Guard correctly.


I do have require 'factory_girl' in my spec/spec_helper.rb.


Here's my Gemfile. Note that I am using "factory_girl_rails" in my Gemfile:

source ''

gem 'rails', '4.1.1'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
# gem 'bcrypt', '~> 3.1.7'

group :development do
gem 'spring'
gem 'sqlite3', '1.3.8'
gem 'minitest'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.0'
gem 'guard-spork', '1.5.0'

group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem 'growl', '1.0.3'
gem 'factory_girl_rails', '4.2.1'

group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'

group :doc do
gem 'sdoc', '0.4.0', require: false


Can you find the error?

Answer Source

I threw a bunch of stuff at this, so it's hard to tell what the precise fix was. One thing I did was switch require 'factory_girl' in spec_helper.rb to require 'factory_girl_rails' and that seems to be the most plausible solution.

Also, I added config.include FactoryGirl::Syntax::Methods to the Rspec.configure block of spec_helper.rb and I think that was also important.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download