我正在尝试学习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)>'我的factories.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 "mhartl@example.com"
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发布于 2010-10-22 03:42:17
也许你应该尝试一下新的语法(参见factory girl的github readme )
FactoryGirl.define :user do |user|
user.name "Michael Hartl"
user.email "mhartl@example.com"
user.password "foobar"
user.password_confirmation "foobar"
end发布于 2012-09-02 07:47:59
根据Factory Girl的最新版本(当前为v4.0.0)重写factories.rb
FactoryGirl.define do
factory :user do
name "Michael Hartl"
email "mhartl@example.com"
password "foobar"
password_confirmation "foobar"
end
end然后从您的用户控制器规范中调用它,如下所示:
FactoryGirl.create(:user)发布于 2011-01-02 14:37:47
我收到了完全相同的错误消息。我刚刚重启了我的Spork服务器和Autotest,一切对我来说都是绿色的。
https://stackoverflow.com/questions/3991052
复制相似问题