【发布时间】:2015-08-24 23:57:48
【问题描述】:
我正在为我的 Rails 应用程序编写非常标准的 RSpec 控制器测试。我遇到的一个问题是简单地测试时间值是否已保留在更新操作中。
在我的控制器中,我有:
def update
if @derp.update_attributes(derp_params)
redirect_to @derp, flash: {success: "Updated derp successfully."}
else
render :edit
end
end
@derp 有一个时间类型的时间属性。我可以在更新操作中测试它的所有其他属性,如下所示:
describe "PATCH #update" do
before do
@attr = {
attribute_1: 5, attribute_2: 6, attribute_3: 7,
time: Time.zone.now
}
end
end
我得到的错误是:
1) DerpsController logged in PATCH #update updates the derps's attributes
Failure/Error: expect(derp.time).to eq(@attr[:time])
expected: 2015-08-24 18:30:32.096943000 -0400
got: 2000-01-01 18:30:32.000000000 +0000
(compared using ==)
Diff:
@@ -1,2 +1,2 @@
-2015-08-24 18:30:32 -0400
+2000-01-01 18:30:32 UTC
我尝试过使用 Timecop 并与 to_s 或 to_i 进行比较...但数据类型 time 的每个属性都完全关闭了。我看过一些帖子说你怎么能期望它在 1 秒内以及如何处理它,但看起来我的这一年已经完全结束了?
这并不难——我只是想测试一下控制器是否可以花时间发送给它并将其保存到数据库中。
我在这里错过了什么?
编辑:几天后这里没有帮助。让我尝试重新迭代正在发生的事情 - 日期被剥离,因为它是 MYSQL 类型的 TIME。注意 2000-01-01...
【问题讨论】:
标签: mysql ruby-on-rails-4 activerecord time rspec