rails 3.1.rc6 + factorygirl+ devise + spec2 => undefined method `Factory' for #<RSpec::Core::ExampleGroup::-Collection of common programming errors
my gem file looks like
source 'http://rubygems.org'
gem 'rails', '3.1.0.rc6'
gem 'sqlite3'
gem 'devise'
gem 'will_paginate'
gem 'therubyracer'
group :assets do
gem 'sass-rails', " ~> 3.1.0.rc"
gem 'coffee-rails', "~> 3.1.0.rc"
gem 'uglifier'
end
gem 'jquery-rails'
group :test do
gem 'turn', :require => false
gem 'rspec-rails'
gem 'webrat'
end
group :development do
gem 'rspec-rails'
gem 'webrat'
gem 'spork'
gem 'factory_girl_rails'
gem 'capybara'
gem 'guard-rspec'
end
my factories.rb
Factory.define :user do |user|
user.username "ccc"
user.email "[email protected]"
user.password "foobar"
user.password_confirmation "foobar"
end
Factory.define :user_hero do |hero|
hero.supername "superman"
hero.association :user
end
my hero_controller_spec looks like this
require 'spec_helper'
describe HerosController do
include Devise::TestHelpers
render_views
before(:each) do
@user = Factory(:user)
# @request.env["devise.mapping"] = :user
# @user = Factory.create(:user)
sign_in @user
@hero_attr = {
:supername => "superman",
}
end
it "should create a new instance with valid attributes" do
@user.heros.create!(@hero_attr)
end
end
My autotest or guard shows up the following message
Failures:
1) HerosController should create a new instance with valid attributes
Failure/Error: @user = Factory(:user)
NoMethodError:
undefined method `Factory' for #
# ./spec/controllers/heros_controller_spec.rb:8:in `block (2 levels) in '
Finished in 0.00726 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/heros_controller_spec.rb:33 # HerosController should create a new instance with valid attributes
-
in spec_helper.rb
add the following
require 'factory_girl' load 'factories.rb'
-
Try this:
@user.HerosController.create!(@hero_attr)
in place of:
@user.create!(@hero_attr)
-
Factory Girl needs to be in the test group otherwise it never gets loaded when you run your specs. Currently it’s loaded only in your development group.
Originally posted 2013-11-09 22:38:22.