Rails 3.2, RSpec, Factory Girl : NameError: uninitialized constant Factory
Asked Answered
S

4

40

Ive been following this introductory to Rails testing and Ive run into an issue I cant seem to find the solution to. Im very familiar with Rails but this is my first foray into testing.

Anyhow, I have a very basic model test, not even fully implemented and when I try and run rspec spec/models/admin_spec.rb. I get the following error in the Admin has a valid factory line (full code below)

Admin has a valid factory
 Failure/Error: Factory.create(:admin).should be_valid
 NameError:
   uninitialized constant Factory
 # ./spec/models/admin_spec.rb:6:in `block (2 levels) in <top (required)>'

I assume FactoryGirl isnt being loaded for some reason but I was under the impression it should be automatically loaded. Below is the full code from my Gemfile, /spec/models/admin_spec.rb and /spec/factories/admins.rb

Thanks very much for your help

Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.2'
gem 'mysql2'
gem 'jquery-rails'
gem 'haml'
gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'bootstrap-sass', '~> 2.0.2'
gem 'capistrano'
gem 'json'
gem "paperclip", '~>3.0'
gem 'airbrake'
gem 'acts_as_list'
gem 'nested_form', :git => 'https://github.com/ryanb/nested_form.git'
gem 'bootstrap-wysihtml5-rails'
gem 'will_paginate', '~> 3.0'
gem 'bootstrap-will_paginate'
gem 'thinking-sphinx', '2.0.10'

gem 'sass-rails', '~> 3.1'
gem 'coffee-rails'
gem 'uglifier'
# gem 'compass'

group :development do
  gem 'awesome_print'
  gem 'wirble'
end

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
end

group :production do
  gem 'execjs'
  gem 'therubyracer'
end

group :test do
  # Pretty printed test output
  gem 'turn', :require => false
  gem 'faker'
  gem 'capybara'
  gem 'guard-rspec'
  gem 'launchy'
end

/spec/factories/admin.rb

require 'faker'

FactoryGirl.define do
  factory :admin do |f|
    f.name Faker::Name.name
    f.email Faker::Internet.email
  end
end

/spec/models/admin_spec.rb

require 'spec_helper'

describe Admin do
  it "has a valid factory" do
    Factory.create(:admin).should be_valid
  end
  it "is invalid without a name"
  it "is invalid without an email"
end
Skimp answered 9/8, 2012 at 1:41 Comment(0)
M
122

It should be FactoryGirl.create instead. Apparently Factory was deprecated and now has been removed, look at the comments in the link you provided :)

Millibar answered 9/8, 2012 at 1:55 Comment(1)
ahhh sigh. Haha, thanks very much. I did a find on the page for the "uninitialized constant Factory error" but didn't read close enough. I got the feeling it'd be something simple. Thanks again.Skimp
P
10

In fact in your spec_helper.rb under Rspec.configure do...end you can add

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end 

This will save you the trouble of the prefixing FactoryGirl. before :build and :create altogether:

require 'spec_helper'

describe Admin do
  it "has a valid factory" do
    create(:admin).should be_valid
  end
  it "is invalid without a name"
  it "is invalid without an email"
end

Refer: FactoryGirl Documentation

Polyploid answered 9/2, 2014 at 18:3 Comment(0)
L
9

This isn't an answer to your question, but I noticed there is an obscure error in your use of Faker with FactoryGirl. f.name and f.email will be the same for every FactoryGirl.create or FactoryGirl.build.

f.name Faker::Name.name
f.email Faker::Internet.email

Add curly braces around the Faker calls so that each reference to a Factory will generate random Faker data.

f.name { Faker::Name.name }
f.email { Faker::Internet.email }
Lavadalavage answered 12/8, 2012 at 5:12 Comment(2)
Ah yup, I did catch that one later on, while I was reviewing my code, but thanks for the heads up anyhow.Skimp
Thank you for this! Stupid mistake but easy to missMarella
G
1

Also, be sure that you are including the require statement in your spec_helper.rb file.

require 'factory_girl_rails'
Googins answered 5/7, 2015 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.