ruby-on-rails – TimeWithZone&Time.zone.now集成测试失败
发布时间:2020-12-16 22:43:27 所属栏目:百科 来源:网络整理
导读:在控制器方法中,当激活电子邮件发送给该用户时,我将用户的变量activation_sent_at设置为等于Time.zone.now.在开发服务器上这似乎是有效的(虽然我的应用程序中的时间表达式在我的计算机本地时间是2个小时). 我想包括一个测试activate_sent_at确实被正确设置的
在控制器方法中,当激活电子邮件发送给该用户时,我将用户的变量activation_sent_at设置为等于Time.zone.now.在开发服务器上这似乎是有效的(虽然我的应用程序中的时间表达式在我的计算机本地时间是2个小时).
我想包括一个测试activate_sent_at确实被正确设置的集成测试.所以我包括了这一行: assert_equal @user.activation_sent_at,Time.zone.now 但是,这会产生错误: No visible difference in the ActiveSupport::TimeWithZone#inspect output. You should look at the implementation of #== on ActiveSupport::TimeWithZone or its members. 我认为这是建议在我的测试中使用另一个表达式为Time.zone.now.我看过不同的来源,包括http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html,但不知道该怎么做.任何建议可能导致这个错误? 附加信息:添加放置Time.zone.now和puts @ interestholder.activation_sent_at确认两个是相等的.不知道是什么导致故障/错误. 解决方法
问题是两个日期彼此非常接近但不一样.您可以使用
assert_in_delta
assert_in_delta @ user.activation_sent_at,Time.zone.now,1.second 对于RSpec,类似的方法将是使用 expect(@ user.activation_sent_at).to be_within(1.second).of Time.zone.now (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |