Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) }
     ArgumentError:
       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) }
end

-

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
    end
end

-

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 'https://rubygems.org'

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'
end

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'
end

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

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

-

Can you find the error?

like image 200
peterhurford Avatar asked May 25 '14 00:05

peterhurford


2 Answers

"ArgumentError: Factory not registered" is also generated if your factory definition is misnamed. Your factory definition might be called phases.rb, but the definition should read: factory :phase do (singular).

like image 85
Jonathan Dueck Avatar answered Oct 20 '22 20:10

Jonathan Dueck


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.

like image 37
peterhurford Avatar answered Oct 20 '22 18:10

peterhurford