Railstutorial.org - undefined `Factory 'method

I am trying to follow at railstutorial.org, and I am currently in chapter 7, where you start using plants: http://railstutorial.org/chapters/modeling-and-viewing-users-two#sec:tests_with_factories

I am using Rails 3.0.1 and ruby-1.9.2-p0

I can't get my rspec tests for life, though, I get an error

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 look 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 "mhartl@example.com" 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 
+9
ruby-on-rails factory-bot
source share
11 answers

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

 FactoryGirl.define :user do |user| user.name "Michael Hartl" user.email "mhartl@example.com" user.password "foobar" user.password_confirmation "foobar" end 
+7
source share

According to the latest version of Factory Girl (currently v4.0.0) rewrite factory .rb

 FactoryGirl.define do factory :user do name "Michael Hartl" email "mhartl@example.com" password "foobar" password_confirmation "foobar" end end 

then call it from the specification of your user controller, like:

 FactoryGirl.create(:user) 
+21
source share

I got the same error message. I just restarted my Spork server and Autotest, and everything turned green for me.

+12
source share

In your specification use

  @user = FactoryGirl(:user) 

instead

  @user = Factory(:user) 
+3
source share

I had this problem, but it was due to the fact that I placed the factory hairstyle in the development section instead of the Gemfile test section. Once, under the test section, it worked. One difference that I note between my post and yours is that my points to 1.0:

 group :test do gem 'rspec-rails', '2.6.1' gem 'webrat', '0.7.1' gem 'factory_girl_rails', '1.0' end 
+2
source share

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

0
source share

My solution: I accidentally included it in a block :development and just had to move it to a block :test

(I listed it here because it can help someone who is not following the rule)

0
source share

I did this, add require 'factory_girl' to test_helper.rb and

 @user = FactoryGirl.create(:user) 
0
source share

For those who find this page now: note where you once used FactoryGirl, now you should use FactoryBot in your tests. From the Thinkbot announcement page:

"Factory_girl was renamed 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

0
source share

FactoryGirl is no longer supported by FactoryBot.

FactoryBot version 5 and later has an obsolete assignment of static attributes.

Therefore, the solution is to declare dynamic attributes when creating the factory.

So the solution is this:

FactoryBot.define do factory :user do name { "test" } email { "test@example.com" } password { "foobar" } password_confirmation { "foobar" } end end

0
source share

I decided to use the newest version of Factory Girl, so I tried to adapt the code. It didn’t work for me, so I used

 gem 'factory_girl_rails', '1.0' 

in gemfile to lock version to 1.0

 bundle update 

restart spork and autotest and it worked.

-one
source share

All Articles