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最新版本(当前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:
我收到了完全相同的错误消息。我刚刚重新启动了 Spork 服务器和自动测试,一切都变绿了。
I got this exact same error message. I just restarted my Spork server and Autotest and everything went green for me.
也许你应该尝试新的语法(请参阅 Factory Girl 的 github readme)
Maybe you should try the new syntax (see github readme of factory girl)
在您的规范中使用
而不是
In your spec use
instead of
我遇到了这个问题,但这是因为我把工厂女孩gem放在了Gemfile的开发部分而不是测试部分下。一旦进入测试部分,它就起作用了。我注意到我的条目和你的条目之间的一个区别是我的指定为 1.0:
I had this problem, but it was because I had placed the factory girl gem under the development section instead of the test section of the Gemfile. Once under the test section, it worked. One difference I note between my entry and yours is that mine specifies 1.0:
对我来说,我必须将
require 'factory_girl'
添加到test_helper.rb
For me I had to add
require 'factory_girl'
totest_helper.rb
我的解决方案:我不小心将其包含在
:development
块中,只需将其移动到:test
块(我已将其列在这里,因为它可能会帮助那些没有正确遵循教程的人)
My solution: I've accidentally included it in the
:development
block, and simply had to move it to the:test
block(I've listed it here, because it might help someone who doesn't follow the tutorial correctly)
我已经这样做了,
将
require 'factory_girl'
添加到test_helper.rb
并I have done so,
add
require 'factory_girl'
totest_helper.rb
and对于那些现在找到此页面的人:请注意您曾经在哪里使用“FactoryGirl”,您现在必须在测试中使用“FactoryBot”。来自thoughtbot公告页面:
更多详细信息请参见:
https://robots.thoughtbot.com/factory_bot
For those finding this page now: note where you once used "FactoryGirl" you must now use "FactoryBot" in your tests. From the thoughtbot announcement page:
More details here:
https://robots.thoughtbot.com/factory_bot
我决定使用最新版本的 Factory Girl,所以我尝试调整代码。对我来说不起作用,所以我
在 Gemfile 中使用将版本锁定在 1.0
重新启动 spork 和自动测试,它起作用了。
I was determined to use the newest version of Factory Girl, so I tried to adapt the code. Didn't work for me, so I used
in the Gemfile to lock the version at 1.0
restart spork and autotest and it worked.