我有一个spec来检查Time.now和object的created_at属性之间的差异的计算。我记下了Time.now,所以这个值是不变的。此外,我已将Time.now设置为created_at,但重新加载对象后此值会更改。怎么可能,如何在对象重新加载后冻结created_at?
这是一个问题的例子:
time = Time.now
=> 2015-03-19 15:50:13 UTC
Time.stubs :now => time
=> #<Expectation:0x9938830 allowed any number of times...
user = User.last
=> #<User:0x000000097a6e40...
user.update_attribute :created_at, Time.now - 1.minute
=> true
user.created_at
=> Thu, 19 Mar 2015 15:49:13 UTC +00:00
Time.now - user.created_at
=> 60.0
Time.now - user.reload.created_at
=> 60.442063277
我使用rails 4.2.0,ruby 2.2.0和rspec 2.14.1
答案 0 :(得分:3)