【发布时间】:2009-11-15 18:16:38
【问题描述】:
我有一些代码基本上显示了在给定表中所做的最后 x(变量,但假设 x 在这里是 20)更新。在其中一个单元测试中,我有这个 sn-p:
EditedItem.push_to_queue(hiddennow)
#create some new entries and save them
20.times{ EditedItem.push_to_queue(random_item) }
Queue.get_entries.each{|entry| assert_not_equal too_far_down, entry}
可能漂亮也可能不漂亮,但它传达了意图。 hiddennow 对象在队列中被推得太远,在调用 get_entries 时不应再返回。
#this works
SearchObject.find(:all, :order => "id desc")
#this does not, unless the 20.times loop has sleep(1) or something
SearchObject.find(:all, :order => "created_at desc")
这被简化了一点,但看起来 20.times 循环添加的东西足够快,以至于 created_at 上的 order by 子句无法区分。我的问题是,我在做一些根本错误的事情吗?如果不是,那么按照这些思路编写测试的更好方法是什么?
【问题讨论】:
标签: mysql ruby-on-rails ruby unit-testing