Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

railstutorial.org - undefined method `Factory'

I'm attempting to follow railstutorial.org, and am currently on Chapter 7, where you start using factories: http://railstutorial.org/chapters/modeling-and-viewing-users-two#sec:tests_with_factories

I'm using Rails 3.0.1 and ruby-1.9.2-p0

I can't for the life of me get my rspec tests to pass though, the error i get is

Failures:
    1) UsersController GET 'show' should be successful
     Failure/Error: @user = Factory(:user)
     undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x00000101cc5608>
 # ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

my factories.rb looks like this:

# By using the symbol ':user', we get Factory Girl to simulate the User model.
Factory.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

and this is my users_controller_spec.rb file:

require 'spec_helper'

describe UsersController do
  render_views

  describe "GET 'show'" do
    before(:each) do
      @user = Factory(:user)
    end
    it "should be successful" do
      get :show, :id => @user
      response.should be_success
    end

here is my Gemfile, if it helps:

source 'http://rubygems.org'

gem 'rails', '3.0.1'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'gravatar_image_tag'

group :development do
  gem 'rspec-rails'
  gem 'annotate-models'
end

group :test do
  gem 'rspec'
  gem 'webrat'
  gem 'spork'
  gem 'factory_girl_rails'
end
like image 319
bobaboba Avatar asked Oct 21 '10 19:10

bobaboba


Video Answer


2 Answers

As per the latest version of Factory Girl (currently v4.0.0) rewrite factories.rb

FactoryGirl.define do 
  factory :user do
    name                  "Michael Hartl"
    email                 "[email protected]"
    password              "foobar"
    password_confirmation "foobar"
  end
end

then call it from your users controller specs as:

FactoryGirl.create(:user)
like image 110
Postscripter Avatar answered Sep 23 '22 08:09

Postscripter


I got this exact same error message. I just restarted my Spork server and Autotest and everything went green for me.

like image 26
Eric Van Joshnon Avatar answered Sep 21 '22 08:09

Eric Van Joshnon