Rails 3 教程:rspec +工厂女孩导轨问题
我一直在关注 Rails 教程(http://railstutorial.org/chapters/beginning,Rails 3 版本),并且在使用 Factory Girl 和 Rspec 时停在第 11 章,我有一个测试未通过我觉得我做错了什么,但我不明白是什么。
首先,Github 上有一个 git 存储库,其中包含未通过该测试的代码。 http://github.com/Monomachus/ch3_static_pages
所以我得到了用户模型
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
has_many :microposts
.
.
.
我得到了微帖子模型
class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user
default_scope :order => 'microposts.created_at DESC'
end
然后我得到了工厂女孩设置
Factory.define :user do |user|
user.name "Michael Hartl"
user.email "[email protected]"
user.password "foobar"
user.password_confirmation "foobar"
end
Factory.define :micropost do |micropost|
micropost.content "Foo bar"
micropost.association :user
end
最后是Rspec代码
require 'spec_helper'
describe Micropost do
.
.
describe "microposts associations" do
before(:each) do
@user = User.create(@attr)
@mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago)
@mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago)
end
it "should have a microposts attribute" do
@user.should respond_to(:microposts)
end
it "should be in the reverse order of appearing" do
@user.microposts.should == [@mp2, @mp1]
end
end
end
我得到了错误,这肯定告诉我我做错了什么。
Failures:
1) Micropost microposts associations should be in the reverse order of appearing
Failure/Error: @user.microposts.should == [@mp2, @mp1]
expected: [#<Micropost id: 2, content: "Foo bar", user_id: nil, created_at: "2010-12-24 12:47:02", update
d_at: "2010-12-24 13:47:02">, #<Micropost id: 1, content: "Foo bar", user_id: nil, created_at: "2010-12-23 13:
47:02", updated_at: "2010-12-24 13:47:02">],
got: [] (using ==)
Diff:
@@ -1,3 +1,2 @@
-[#<Micropost id: 2, content: "Foo bar", user_id: nil, created_at: "2010-12-24 12:47:02", updated_at: "20
10-12-24 13:47:02">,
- #<Micropost id: 1, content: "Foo bar", user_id: nil, created_at: "2010-12-23 13:47:02", updated_at: "20
10-12-24 13:47:02">]
+[]
# ./spec/models/micropost_spec.rb:42:in `block (3 levels) in <top (required)>'
正如你所看到的,甚至 user_id 属性也没有正确设置 +
显然 @user.microposts 没有任何元素。
请帮我解决这个问题,谢谢。
i've been following the Rails tutorial (http://railstutorial.org/chapters/beginning , Rails 3 version), and i've stopped at 11th chapter when using Factory Girl and Rspec, I have a test that isn't passing and I feel I'm doing something wrong but I don't see what.
First of all there is a git repository on Github with the code that doesn't pass that test.
http://github.com/Monomachus/ch3_static_pages
So I got users model
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
has_many :microposts
.
.
.
I got microposts model
class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user
default_scope :order => 'microposts.created_at DESC'
end
Then I got Factory girl settings
Factory.define :user do |user|
user.name "Michael Hartl"
user.email "[email protected]"
user.password "foobar"
user.password_confirmation "foobar"
end
Factory.define :micropost do |micropost|
micropost.content "Foo bar"
micropost.association :user
end
And finally Rspec code
require 'spec_helper'
describe Micropost do
.
.
describe "microposts associations" do
before(:each) do
@user = User.create(@attr)
@mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago)
@mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago)
end
it "should have a microposts attribute" do
@user.should respond_to(:microposts)
end
it "should be in the reverse order of appearing" do
@user.microposts.should == [@mp2, @mp1]
end
end
end
And I got the error which definitely tells me that I do something wrong.
Failures:
1) Micropost microposts associations should be in the reverse order of appearing
Failure/Error: @user.microposts.should == [@mp2, @mp1]
expected: [#<Micropost id: 2, content: "Foo bar", user_id: nil, created_at: "2010-12-24 12:47:02", update
d_at: "2010-12-24 13:47:02">, #<Micropost id: 1, content: "Foo bar", user_id: nil, created_at: "2010-12-23 13:
47:02", updated_at: "2010-12-24 13:47:02">],
got: [] (using ==)
Diff:
@@ -1,3 +1,2 @@
-[#<Micropost id: 2, content: "Foo bar", user_id: nil, created_at: "2010-12-24 12:47:02", updated_at: "20
10-12-24 13:47:02">,
- #<Micropost id: 1, content: "Foo bar", user_id: nil, created_at: "2010-12-23 13:47:02", updated_at: "20
10-12-24 13:47:02">]
+[]
# ./spec/models/micropost_spec.rb:42:in `block (3 levels) in <top (required)>'
As you can see even the user_id property is not set correctly +
apparently @user.microposts doesn't have any elements.
Please help me with this issue thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案很简单:)
我在微帖子规范中包含了微帖子关联。
显然
@attr不包含用户属性,而是包含微帖子属性,当然@user = nil,然后一切就有意义了。因此,如果您确实遇到同样的问题,请将此代码包含到用户规范中。
现在我所有的测试都通过了:)
Well the answer was simple :)
I included microposts associations in the Micropost spec.
And clearly
@attr did not contain the user properties but the micropost properties and of course @user = nil and then everything makes sense. So if you do have the same problem, include this code into User spec.
Now all my tests pass :)
当我完成分页章节时,教程正在使用 Faker 创建 100 个示例用户(第 390 页上列出了 10.25 个),在 RubyMine 中我可以看到我的测试失败了,因为程序在重复的用户电子邮件上引发了异常地址(具有唯一约束)。 user_spec.rb 第 8 行的 @attr 有:email => “[email protected]",但是这会引发异常,因为它是重复的电子邮件(我猜是因为 Faker 已经创建了它)。
对我来说,修复方法是从第 8 行复制 @attr 并将其粘贴到描述“微帖子关联”块 (user_spec.rb) 中,然后将电子邮件地址更改为 :email => “[电子邮件受保护]”。我确信这完全是黑客行为,但我是个菜鸟。
更新:
对我来说另一个修复是注释掉 @user = User.create(@attr) 行,然后简单地创建 @mp1 和 @mp2。
By the time I had finished the pagination chapter, the tutorial was creating 100 sample users using Faker (listing 10.25 on page 390), and in RubyMine I was able to see my test was failing because the program was throwing an exception on duplicate user email address (which has a unique constraint). The @attr on line 8 of user_spec.rb has :email => "[email protected]", however this throws an exception since it's a duplicate email (I guess because Faker has already created it).
For me the fix was to copy @attr from line 8 and paste it into the describe "micropost associations" block (user_spec.rb), and change the email address to :email => "[email protected]". I'm sure this is a total hack but I'm a n00b.
Update:
Another fix for me was to comment out the line @user = User.create(@attr), and simply create @mp1 and @mp2.
尽管我在 user_spec.rb 中已经有了“微帖子关联”,但我在本节中也遇到了测试失败。结果我需要重新启动 spork 和自动测试,以便让它们使用factory.rb 中的新“micropost”工厂。
I was also getting test failure in this section, even though I already had "micropost associations" in user_spec.rb. Turns out I needed to restart spork and autotest in order to get them to use the new "micropost" factory in factories.rb.