2014-08-29 25 views
0

我想不通爲什麼rails datetime不匹配。下面的測試失敗,因爲我的created_at日期是不相同的,但我不知道爲什麼爲什麼datetime不是預期的和如何解決?

測試

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)>' 

任何想法,將不勝感激。

謝謝。

回答

0

經過一番調查後,似乎在保存到MySQL時會失去一些精確度。爲了糾正這個問題,我所做的只是course.reload,精度現在匹配。

相關問題