Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory not registered: user

I am following Michal Hartls Rails tutorial Chapter 7. I have installed the Factory girl gem. I keep getting this error when I run my test

Failures:

 1) UsersController GET 'show' should be successful
     Failure/Error: @user = FactoryGirl.create(:user)
     ArgumentError:
       Factory not registered: user
     # ./spec/controllers/users_controller_spec.rb:10:in `block (3 levels) in <top (required)>'

Finished in 0.66336 seconds 42 examples, 1 failure

Failed examples:

rspec ./spec/controllers/users_controller_spec.rb:14 # UsersController GET 'show' should be successful

Gemfile

group :test do
  gem 'autotest', '4.4.6'
  gem "autotest-growl", "~> 0.2.16"
  gem "autotest-rails", "~> 4.1.2"
  gem "rspec", "~> 2.9.0"
  gem 'rspec-rails',  '2.9.0'
  gem 'webrat', '0.7.3'
  gem "spork",  '0.9.0'
  gem 'annotate', '~> 2.4.1.beta'
  gem "factory_girl", "~> 3.2.0"
end
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', :platform =>

My user_controller_spec.rb is

require 'spec_helper'

describe UsersController do

  render_views

  describe "GET 'show'" do
    before(:each) do
    @user = FactoryGirl.create(:user)
    end

    it "should be successful" do
      get :show, :id => @user
      response.should be_success
    end
  end

  describe "GET 'new'" do
    it "should be successful" do
      get :new
      response.should be_success
    end

    it "should have the right title" do
      get :new
      response.should have_selector('title', :content => "Sign up")
    end
  end
end

My factories.rb is

FactoryGirl.create :user do |user|
  user.name                   "Mark Brown"
  user.email                 "[email protected]"
  user.password               "foobar"
  user.password_confirmation  "foobar"
end

I changed

Factory(:name)

to

FactoryGirl.create(:user)

on line 10 in my user_controller_spec.rb becuase I was getting a deprication warning. I thought that would fix it but now im just getting.

Factory not registered: user

Can someone tell me what going on?

like image 494
cw.prime Avatar asked May 02 '12 04:05

cw.prime


3 Answers

I have already fixed it.

The solution was to change gem 'factory_girl' to gem 'factory_girl_rails' in my Gemfile.

like image 109
Gil Gomes Avatar answered Nov 15 '22 12:11

Gil Gomes


I had the same error when I first used FactoryGirl in that Chapter and all I needed to do was to restart the server and Spork if you're using it. Hope it helps

like image 45
rkrdo Avatar answered Nov 15 '22 11:11

rkrdo


what if you define factory first, not create it? try this:

FactoryGirl.define do
  factory :user do
    name                  "Mark Brown"
    email                 "[email protected]"
    password              "foobar"
    password_confirmation "foobar"
  end 
end
like image 6
Evgeniy Kelyarsky Avatar answered Nov 15 '22 11:11

Evgeniy Kelyarsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!