Rails assert_equal 并不总是适用于 DateTimes

发布于 2024-10-29 22:03:44 字数 484 浏览 0 评论 0原文

使用 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)。那么为什么它们不相等呢?

这是数据库中的标准日期时间字段,我认为它只存储到右第二个?

我可以通过将它们转换为整数或使用范围为 1 分钟的 assert_in_delta 来使其通过。但只是想知道。

顺便说一句,这是 Rails 2.3.8 和 MySQL。

I get an error in my functional test when using 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>.

Notice that the two show the same time and time zone. I checked and they are the same class type (ActiveSupport::TimeWithZone). So why aren't they equal?

It's a standard DateTime field in the database, which I think is only stored down to the second right?

I can get it to pass by converting them to integers or using assert_in_delta with a range of 1 minute. But was just wondering.

Btw this is Rails 2.3.8 and MySQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

盛装女皇 2024-11-05 22:03:44

我也遇到同样的错误。看起来这是报告回来的2009 年:

我之前在测试中见过这种情况 - 通常是由于数据库的时间分辨率与系统不同而引起的。因此,即使两次打印相同,一次实际上是(例如)15:45:32.012445362,而数据库加载回 15:45:32,这并不相等。

建议的解决方案对我有用:

在您的测试中,您可以在比较之前尝试强制转换 to_a ; to_a 表示中未返回 usec 值:

I'm getting the same error too. It looks like this was 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.

The suggested solution, which worked for me:

In your tests, you can try coercing to_a before comparing; usec value isn't returned in the to_a representation:

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