Rspec预期并且相同但仍未通过测试

时间:2014-12-02 10:37:37

标签: ruby-on-rails ruby rspec

这是测试代码:

it "should return the latest test result object for user" do
  value = Diagnostics.latest_test_result_object_for("urine color", @user)
  expect(value).to eq(@labtestresult3)
end

这是我得到的结果:

expected: [{"id"=>3, "lab_test_id"=>4, "user_id"=>1, "user_age"=>34, "numeric_value"=>nil, "boolean_value"=>nil, "string_value"=>"pale yellow", "lab_test_reference_label"=>nil, "lab_test_severity_level"=>nil, "lab_test_provider_id"=>1, "lab_test_method_id"=>1, "lab_test_date"=>Tue, 02 Dec 2014 16:03:33 IST +05:30, "created_at"=>Tue, 02 Dec 2014 16:03:33 IST +05:30, "updated_at"=>Tue, 02 Dec 2014 16:03:33 IST +05:30}]
     got: [{"id"=>3, "lab_test_id"=>4, "user_id"=>1, "user_age"=>34, "numeric_value"=>nil, "boolean_value"=>nil, "string_value"=>"pale yellow", "lab_test_reference_label"=>nil, "lab_test_severity_level"=>nil, "lab_test_provider_id"=>1, "lab_test_method_id"=>1, "lab_test_date"=>Tue, 02 Dec 2014 16:03:33 IST +05:30, "created_at"=>Tue, 02 Dec 2014 16:03:33 IST +05:30, "updated_at"=>Tue, 02 Dec 2014 16:03:33 IST +05:30}]

(compared using ==)

Diff:
./spec/lib/diagnostics_spec.rb:51:in `block (2 levels) in <top (required)>'
-e:1:in `<main>'

1 example, 1 failure, 0 passed

Finished in 3.854677444 seconds

我不明白这是什么问题。

请帮忙。

1 个答案:

答案 0 :(得分:1)

如果在您的规范中您正在使用时间,那么最好使用像timecop这样的宝石。在您的情况下,我认为问题是lab_test_date / created_at / updated_at比较,因为在操作执行和期望检查之间传递了一些时间。您可以使用timecop以这种简单方式解决此问题:

before(:all) do
  Timecop.freeze(Time.now)
end

after(:all) do
  Timecop.return
end