Railstutorial.org - 未定义的方法“Factory”
我正在尝试关注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
I'm attempting to follow railstutorial.org, and am currently on Chapter 7, where you start using factories: http://railstutorial.org/chapters/modeling-and-viewing-users-two#sec:tests_with_factories
I'm using Rails 3.0.1 and ruby-1.9.2-p0
I can't for the life of me get my rspec tests to pass though, the error i get is
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)>'
my factories.rb looks like this:
# 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
and this is my users_controller_spec.rb
file:
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
here is my Gemfile, if it helps:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(10)
也许你应该尝试新的语法(请参阅 Factory Girl 的 github readme)
FactoryGirl.define :user do |user|
user.name "Michael Hartl"
user.email "[email protected]"
user.password "foobar"
user.password_confirmation "foobar"
end
对于那些现在找到此页面的人:请注意您曾经在哪里使用“FactoryGirl”,您现在必须在测试中使用“FactoryBot”。来自thoughtbot公告页面:
“我们正在将factory_girl重命名为factory_bot(并将factory_girl_rails重命名为
工厂_机器人_rails)。现在与factory_girl 具有相同的功能
以不同的名称。”
更多详细信息请参见:
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
根据Factory Girl最新版本(当前v4.0.0)
重写factories.rb,
然后从您的用户控制器规范中调用它,如下所示:
As per the latest version of Factory Girl (currently v4.0.0)
rewrite factories.rb
then call it from your users controller specs as: