我在使用 assert_equal
时在功能测试中遇到错误:
1) [31mFailure[0m:
test_should_allow_dealer_to_extend_offer:21
<Thu, 14 Apr 2011 23:59:59 PDT -07:00> expected but was
<Thu, 14 Apr 2011 23:59:59 PDT -07:00>.
请注意,两者显示相同的时间和时区。我检查过它们是相同的类类型 (ActiveSupport::TimeWithZone)。那么为什么它们不相等呢?
它是数据库中的标准 DateTime 字段,我认为它只存储到第二个右边?
我可以通过将它们转换为整数或使用范围为 1 分钟的 assert_in_delta
来让它通过。但只是想知道。
顺便说一句,这是 Rails 2.3.8 和 MySQL。
最佳答案
我也遇到了同样的错误。看起来这是 reported back in 2009 :
I've seen this happen in tests before - typically caused by the database having a different time resolution than the system. So even though the two times print identically, one is really (for instance) 15:45:32.012445362 and the DB loads back 15:45:32, which doesn't compare as equal.
建议的解决方案,对我有用:
In your tests, you can try coercing to_a before comparing; usec value isn't returned in the to_a representation:
关于ruby-on-rails - Rails assert_equal 并不总是适用于 DateTimes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5556726/