监控进程干扰Factory的实例初始化
在我的项目中,用户将拥有许多项目,这些项目在创建时的 onshelf_at 属性默认为 DateTime.now
。
# item.rb
class Item < ActiveRecord::Base
before_create :calculate_onshelf_time
default_scope :order => 'items.onshelf_at DESC'
def calculate_onshelf_time
self.onshelf_at = DateTime.now
end
end
在用户模型测试中,我试图说服自己检索到的订单确实是 items.onshelf_at DESC
。所以我做了下面的片段,但结果却相反。 (即[@item1, @item2]
)
# spec/models/user_spec.rb
before :each do
@user = User.create(@attr)
@item1 = Factory(:item, :owner=>@user, :onshelf_at => 2.days.ago, :created_at => 2.days.ago)
@item2 = Factory(:item, :owner=>@user, :onshelf_at => 1.day.ago, :created_at => 1.day.ago)
end
it "should have the right items in the right order" do
@user.items.should == [@item2, @item1]
end
我检查了控制台,发现onshelf_at没有监听Factory Girl的实例初始化。相反,它遵循 before_create 规则,并重视测试运行的时间!
Failure/Error: @user.items.should == [@item2, @item1]
expected: [#<Item id: 2, description: "this is an item", img_link: "http://www.example.com/photos/some_pic.jpg", category_id: 5, onshelf: true, created_at: "2011-11-20 11:19:15", updated_at: "2011-11-21 11:19:15", onshelf_at: "2011-11-21 11:19:15", owner_id: 1>, #<Item id: 1, description: "this is an item", img_link: "http://www.example.com/photos/some_pic.jpg", category_id: 5, onshelf: true, created_at: "2011-11-19 11:19:15", updated_at: "2011-11-21 11:19:15", onshelf_at: "2011-11-21 11:19:15", owner_id: 1>]
got: [#<Item id: 1, description: "this is an item", img_link: "http://www.example.com/photos/some_pic.jpg", category_id: 5, onshelf: true, created_at: "2011-11-19 11:19:15", updated_at: "2011-11-21 11:19:15", onshelf_at: "2011-11-21 11:19:15", owner_id: 1>, #<Item id: 2, description: "this is an item", img_link: "http://www.example.com/photos/some_pic.jpg", category_id: 5, onshelf: true, created_at: "2011-11-20 11:19:15", updated_at: "2011-11-21 11:19:15", onshelf_at: "2011-11-21 11:19:15", owner_id: 1>] (using ==)
我该如何解决这个问题?
In my project, the user will have many items, which have an onshelf_at attribute default to DateTime.now
at its creation.
# item.rb
class Item < ActiveRecord::Base
before_create :calculate_onshelf_time
default_scope :order => 'items.onshelf_at DESC'
def calculate_onshelf_time
self.onshelf_at = DateTime.now
end
end
In the user model test, I tried to convince myself the retrieved order is indeed items.onshelf_at DESC
. So I made the following snippet, but the result turned out to be reverse. (namely [@item1, @item2]
)
# spec/models/user_spec.rb
before :each do
@user = User.create(@attr)
@item1 = Factory(:item, :owner=>@user, :onshelf_at => 2.days.ago, :created_at => 2.days.ago)
@item2 = Factory(:item, :owner=>@user, :onshelf_at => 1.day.ago, :created_at => 1.day.ago)
end
it "should have the right items in the right order" do
@user.items.should == [@item2, @item1]
end
I checked the console, and found that onshelf_at wasn't listening to the instance initialization of Factory Girl. In its stead, it followed before_create rule, and valued to the time when test was run!
Failure/Error: @user.items.should == [@item2, @item1]
expected: [#<Item id: 2, description: "this is an item", img_link: "http://www.example.com/photos/some_pic.jpg", category_id: 5, onshelf: true, created_at: "2011-11-20 11:19:15", updated_at: "2011-11-21 11:19:15", onshelf_at: "2011-11-21 11:19:15", owner_id: 1>, #<Item id: 1, description: "this is an item", img_link: "http://www.example.com/photos/some_pic.jpg", category_id: 5, onshelf: true, created_at: "2011-11-19 11:19:15", updated_at: "2011-11-21 11:19:15", onshelf_at: "2011-11-21 11:19:15", owner_id: 1>]
got: [#<Item id: 1, description: "this is an item", img_link: "http://www.example.com/photos/some_pic.jpg", category_id: 5, onshelf: true, created_at: "2011-11-19 11:19:15", updated_at: "2011-11-21 11:19:15", onshelf_at: "2011-11-21 11:19:15", owner_id: 1>, #<Item id: 2, description: "this is an item", img_link: "http://www.example.com/photos/some_pic.jpg", category_id: 5, onshelf: true, created_at: "2011-11-20 11:19:15", updated_at: "2011-11-21 11:19:15", onshelf_at: "2011-11-21 11:19:15", owner_id: 1>] (using ==)
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个快速解决方法是向您的
calculate_onshelf_time
方法添加一个条件:但是 - 您甚至不需要所有这些。如果可以(也就是说,如果您可以控制架构),请将
onshelf_at
属性替换为created_at
列,Rails 将在创建时自动设置该列。创建。如果您使用迁移:会将
created_at
和updated_at
时间戳添加到模型中。A quick fix would be to add a condition to your
calculate_onshelf_time
method:But - you don't even need all this. If you can (that is, if you have control over the schema), replace the
onshelf_at
attribute with acreated_at
column, which will automatically be set by Rails at the time of creation. If you're using migrations:will add
created_at
andupdated_at
timestamps to the model.