ruby-on-rails - Rails assert_equal 并不总是适用于 DateTimes

标签 ruby-on-rails testing minitest

我在使用 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/

相关文章:

ruby-on-rails - 使用多个参数问题创建

ruby-on-rails - Ubuntu 12.04 ruby 1.9.3 Rails 4.0 : Could not find 'atomic'

django - 对基于 Django 的项目中的 A/B 测试有什么想法吗?

ruby - Ruby测试期望命令会被执行

ruby-on-rails - ci_reporter 1.9.2 + minitest 4.7.5 = 没有 xml 报告

ruby-on-rails - Devise in Rails 4 的自定义 session Controller

mysql - 使用 AWS OpsWorks 覆盖 RDS 中的 my.conf 模板

java - 无法单击 ExtJS 下拉按钮并选择列表元素 - Selenium Webdriver Java

java - JUnit 的@Ignore

debugging - 如何找出 Ruby 中使用 at_exit 方法注册了哪些回调?