监控进程干扰Factory的实例初始化

发布于 2024-12-17 09:23:20 字数 2184 浏览 1 评论 0原文

在我的项目中,用户将拥有许多项目,这些项目在创建时的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十秒萌定你 2024-12-24 09:23:20

一个快速解决方法是向您的 calculate_onshelf_time 方法添加一个条件:

 self.onshelf_at = DateTime.now if self.onshelf_at.nil?

但是 - 您甚至不需要所有这些。如果可以(也就是说,如果您可以控制架构),请将 onshelf_at 属性替换为 created_at 列,Rails 将在创建时自动设置该列。创建。如果您使用迁移:

 create_table :foo do |t|
   # ...
   t.timestamps
 end

会将 created_atupdated_at 时间戳添加到模型中。

A quick fix would be to add a condition to your calculate_onshelf_time method:

 self.onshelf_at = DateTime.now if self.onshelf_at.nil?

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 a created_at column, which will automatically be set by Rails at the time of creation. If you're using migrations:

 create_table :foo do |t|
   # ...
   t.timestamps
 end

will add created_at and updated_at timestamps to the model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文