railstutorial.org - undefined method `Factory'
Asked Answered
A

10

10

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
Amberly answered 21/10, 2010 at 19:14 Comment(0)
A
7

Maybe you should try the new syntax (see github readme of factory girl)

FactoryGirl.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end
Arsphenamine answered 21/10, 2010 at 19:42 Comment(0)
B
21

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)
Bilek answered 1/9, 2012 at 23:47 Comment(0)
H
12

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

Heptarchy answered 2/1, 2011 at 6:37 Comment(1)
+1 This fixed the problem for me to with the FactoryGirl version from the tutorial.Franny
A
7

Maybe you should try the new syntax (see github readme of factory girl)

FactoryGirl.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end
Arsphenamine answered 21/10, 2010 at 19:42 Comment(0)
L
3

In your spec use

  @user = FactoryGirl(:user)

instead of

  @user = Factory(:user)
Lebanon answered 5/11, 2012 at 3:1 Comment(2)
Instead of FactoryGirl(:user), useFactoryGirl.create(:user) as suggested by @BilekMark
create is implicitly calledLebanon
F
2

I had this problem, but it was because I had placed the factory girl gem under the development section instead of the test section of the Gemfile. Once under the test section, it worked. One difference I note between my entry and yours is that mine specifies 1.0:

group :test do
  gem 'rspec-rails', '2.6.1'
  gem 'webrat', '0.7.1'
  gem 'factory_girl_rails', '1.0'
end
Finis answered 21/1, 2012 at 19:37 Comment(0)
G
0

For me I had to add require 'factory_girl' to test_helper.rb

Gnarled answered 7/11, 2011 at 16:19 Comment(0)
U
0

My solution: I've accidentally included it in the :development block, and simply had to move it to the :test block

(I've listed it here, because it might help someone who doesn't follow the tutorial correctly)

Unsuspecting answered 3/3, 2012 at 20:35 Comment(0)
A
0

I have done so, add require 'factory_girl' to test_helper.rb and

@user = FactoryGirl.create(:user)
Annabell answered 15/1, 2014 at 12:19 Comment(0)
S
0

For those finding this page now: note where you once used "FactoryGirl" you must now use "FactoryBot" in your tests. From the thoughtbot announcement page:

"We’re renaming factory_girl to factory_bot (and factory_girl_rails to factory_bot_rails). All the same functionality of factory_girl, now under a different name."

More details here:

https://robots.thoughtbot.com/factory_bot

Strongroom answered 28/5, 2018 at 15:22 Comment(0)
D
-1

I was determined to use the newest version of Factory Girl, so I tried to adapt the code. Didn't work for me, so I used

gem 'factory_girl_rails', '1.0'

in the Gemfile to lock the version at 1.0

bundle update

restart spork and autotest and it worked.

Duarte answered 19/8, 2012 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.