【发布时间】:2013-10-01 22:11:06
【问题描述】:
我正在尝试通过[the original object].to_json 和响应正文之间的相等性来测试带有 rspec(在 Rails 中)的 JSON 响应。问题是响应 JSON 字符串中的 updated_at 和 created_at 字段与原始对象中的相同字段相差几毫秒,因此测试失败。
测试:
lesson = FactoryGirl.create(:lesson)
request.accept = "application/json"
get :show, {:id => lesson.to_param, :location_id => lesson.location_id}, valid_session
response.body.should eq lesson.to_json
结果:
expected: "{\"id\":1,\"location_id\":1,\"day_number\":5,\"time\":\"17:00\",\"min_age\":4,\"max_age\":10,\"created_at\":\"2013-10-02T00:51:53.870+03:00\",\"updated_at\":\"2013-10-02T00:51:53.870+03:00\"}"
got: "{\"id\":1,\"location_id\":1,\"day_number\":5,\"time\":\"17:00\",\"min_age\":4,\"max_age\":10,\"created_at\":\"2013-10-02T00:51:53.000+03:00\",\"updated_at\":\"2013-10-02T00:51:53.000+03:00\"}"
请注意,这两个字符串是相等的,除了期望中的 870 毫秒(以及实际结果中的 000)。
我该如何测试?
【问题讨论】:
标签: ruby-on-rails ruby rspec rspec-rails