我正在尝试遵循railstutorial.org,目前正在使用第7章,从这里开始使用工厂:http://railstutorial.org/chapters/modeling-and-viewing-users-two#sec:tests_with_factories

我正在使用Rails 3.0.1和ruby-1.9.2-p0

我无法终生让我的rspec测试通过,但我得到的错误是

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)>'

我的factory.rb看起来像这样:
# 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

这是我的users_controller_spec.rb文件:
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

这是我的Gemfile,如果有帮助的话:
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

最佳答案

也许您应该尝试新的语法(请参阅工厂女孩的github readme)

FactoryGirl.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

关于ruby-on-rails - railstutorial.org-未定义的方法 `Factory',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3991052/

10-16 03:52