两个 ActiveSupport::TimeWithZone 对象之间的比较失败 [英] Comparison between two ActiveSupport::TimeWithZone objects fails

查看:110
本文介绍了两个 ActiveSupport::TimeWithZone 对象之间的比较失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的测试套件中,我有一个失败的测试.

In my test suite, I have a failing test.

expected[0]['date'] 来自 SomeModel.first.created_at

在调试控制台中,我有以下内容:

In a debugging console, I have the following:

> expected[0]['date']
=> Tue, 25 Mar 2014 16:01:45 UTC +00:00
> res[0]['date']
=> Tue, 25 Mar 2014 16:01:45 UTC +00:00
> res[0]['date'] == expected[0]['date']
=> false # wtf
> res[0]['date'].class
=> ActiveSupport::TimeWithZone
> expected[0]['date'].class
=> ActiveSupport::TimeWithZone
>

这怎么可能?

我试图重现这个问题(我认为 TimeWithZone 上的 == 运算符可能检查引用,或类似的东西,但没有......):

I've tried to reproduce this problem (I tought maybe the == operator on TimeWithZone checks the reference, or something like this, but no...) :

> t1 = Time.zone.at(0)
=> Thu, 01 Jan 1970 00:00:00 UTC +00:00
> t2 = Time.zone.parse(t1.to_s)
=> Thu, 01 Jan 1970 00:00:00 UTC +00:00
> t1 == t2
=> true
> t1.class
=> ActiveSupport::TimeWithZone
> t2.class
=> ActiveSupport::TimeWithZone

<小时>

编辑:更多测试...

> res[0]['date'].eql?(expected[0]['date'])
=> false
> res[0]['date'].zone
=> "UTC"
> expected[0]['date'].zone
=> "UTC"
> expected[0]['date'].getlocal
=> 2014-03-25 16:01:45 +0000
> res[0]['date'].getlocal
=> 2014-03-25 16:01:45 +0000
> res[0]['date'].hash
=> -3455877575500291788
> expected[0]['date'].hash
=> -3819233736262144515
>
> t1.hash
=> 2279159074362527997
> t2.hash
=> 2279159074362527997

# inspect...
> expected[0]['date'].inspect
=> "Tue, 25 Mar 2014 16:39:01 UTC +00:00"
> res[0]['date'].inspect
=> "Tue, 25 Mar 2014 16:39:01 UTC +00:00"

看起来比较是基于哈希对象的.为什么 res 和 expected 有不同的 hashes ?

Looks like the comparison is based on the hash object. Why res and expected have different hashes ?

推荐答案

答案 #1 rake db:test:prepare

首先,尝试删除测试数据库并重新创建它,然后运行 ​​rake db:test:prepare.这在过去为我解决了这个问题我知道这有点蹩脚的答案,但值得一试.

First, attempt dropping the test database and recreating it, and then running rake db:test:prepare. This has resolved this issue for me in the past I know this is a bit of a lame answer, but it is worth a shot.

答案 #2 Spring + Rspec + Shoulda 匹配器

Answer #2 Spring + Rspec + Shoulda matchers

如果在安装 Spring 后出现此问题,请查看此 Github 线程,这可能会导致测试失败:https://github.com/rails/spring/issues/209

If having this issue after installing Spring, please checkout this Github Thread, which can cause tests to fail: https://github.com/rails/spring/issues/209

此问题仅在我将 Spring 添加到我的项目后才开始出现.添加 gem 'shoulda', require: false 并手动添加 require 'shoulda/matchers' 到我的 spec_helper.rb 解决了问题

This issue only started occurring for me after adding Spring to my project. Adding gem 'shoulda', require: false and manually adding require 'shoulda/matchers' to my spec_helper.rb resolved the issues

回答 #3 时间警察

如果仍有问题,请查看 Timecop gem 并在日期比较前后冻结时间.https://github.com/travisjeffery/timecop

If still having issues, checkout the Timecop gem and freeze time around date comparisons. https://github.com/travisjeffery/timecop

这篇关于两个 ActiveSupport::TimeWithZone 对象之间的比较失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆