相关疑难解决方法(0)

无法将时间与RSpec进行比较

我正在使用Ruby on Rails 4和rspec-rails gem 2.14.对于我的对象,我想updated_at在控制器动作运行后将当前时间与对象属性进行比较,但由于规范没有通过,我遇到了麻烦.也就是说,鉴于以下是规范代码:

it "updates updated_at attribute" do
  Timecop.freeze

  patch :update
  @article.reload
  expect(@article.updated_at).to eq(Time.now)
end
Run Code Online (Sandbox Code Playgroud)

当我运行上面的规范时,我收到以下错误:

Failure/Error: expect(@article.updated_at).to eq(Time.now)

   expected: 2013-12-05 14:42:20 UTC
        got: Thu, 05 Dec 2013 08:42:20 CST -06:00

   (compared using ==)
Run Code Online (Sandbox Code Playgroud)

如何使规范通过?


注意:我还尝试了以下(注意utc添加):

it "updates updated_at attribute" do
  Timecop.freeze

  patch :update
  @article.reload
  expect(@article.updated_at.utc).to eq(Time.now)
end
Run Code Online (Sandbox Code Playgroud)

但规格仍然没有通过(注意"得到"的价值差异):

Failure/Error: expect(@article.updated_at.utc).to eq(Time.now)

   expected: 2013-12-05 14:42:20 UTC
        got: 2013-12-05 14:42:20 UTC

   (compared using ==)
Run Code Online (Sandbox Code Playgroud)

ruby comparison time rspec ruby-on-rails

113
推荐指数
6
解决办法
4万
查看次数

标签 统计

comparison ×1

rspec ×1

ruby ×1

ruby-on-rails ×1

time ×1