【问题标题】:Why is datetime not what expected and how to fix?为什么 datetime 不是预期的以及如何解决?
【发布时间】:2014-08-29 08:52:51
【问题描述】:

我不明白为什么 rails datetime 不匹配。以下测试失败,因为我的 created_at 日期不一样,但我不知道为什么

测试

it "renders the course as json" do
  course = create(:course)
  get :index, format: :json
  expect(response.body).to eq [course].to_json
end

输出

CoursesController
  GET #index - json
    renders the #index view (FAILED - 1)

Failures:

  1) CoursesController GET #index - json renders the #index view
     Failure/Error: expect(response.body).to eq [course].to_json

       expected: "[{\"id\":2,\"partner_id\":2,\"name\":\"MyCourse 1\",\"created_at\":\"2014-08-29T10:49:06.205+02:00\",\"updated_at\":\"2014-08-29T10:49:06.205+02:00\",\"status\":\"active\"}]"
            got: "[{\"id\":2,\"partner_id\":2,\"name\":\"MyCourse 1\",\"created_at\":\"2014-08-29T10:49:06.000+02:00\",\"updated_at\":\"2014-08-29T10:49:06.000+02:00\",\"status\":\"active\"}]"

       (compared using ==)
     # ./spec/controllers/courses_controller_spec.rb:59:in `block (3 levels) in <top (required)>'

任何想法都将不胜感激。

谢谢。

【问题讨论】:

    标签: ruby-on-rails-4 rspec


    【解决方案1】:

    经过一番调查,似乎在保存到 MySQL 时会丢失一些精度。为了纠正这个问题,我所做的只是course.reload,现在精度匹配了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2010-11-22
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 2014-03-20
      相关资源
      最近更新 更多