bicepjai
bicepjai

Reputation: 1665

rails 3.1.rc6 + factorygirl+ devise + spec2 => undefined method `Factory' for #<RSpec::Core::ExampleGroup::

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 #<RSpec::Core::ExampleGroup::Nested_1:0x000001028a3998>
     # ./spec/controllers/heros_controller_spec.rb:8:in `block (2 levels) in <top (required)>'

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

Upvotes: 0

Views: 1623

Answers (4)

Amrit Dhungana
Amrit Dhungana

Reputation: 4485

Try this

 @user = FactoryGirl(:user)

or

@user = FactoryGirl.create(:user)

Upvotes: 0

bicepjai
bicepjai

Reputation: 1665

in spec_helper.rb

add the following

require 'factory_girl'
load 'factories.rb'

Upvotes: 1

Vizjerai
Vizjerai

Reputation: 1972

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.

Upvotes: 0

zengr
zengr

Reputation: 38899

Try this:

@user.HerosController.create!(@hero_attr) 

in place of:

@user.create!(@hero_attr)

Upvotes: 0

Related Questions