2014-12-02 46 views
1

這是測試代碼: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 

我不明白是什麼問題。

請幫忙。

+0

我敢打賭上略有不同的日期時間(不顯示毫秒) – apneadiving 2014-12-02 10:39:06

+0

@apneadiving我想你是對的,很奇怪怎麼我現在用的是相同的:你可以在這個簡單的方式與timecop解決問題對象在這兩個地方,我沒有創建一個新的,nvm我用這個'expect(group_arr [0] .map(&:to_s))。eq(@ labtestresult3.attributes.map(&:to_s))' – aelor 2014-12-02 10:50:18

回答

1

如果在您的規格中您正在使用時間,最好使用像timecop這樣的寶石。在你的情況下,我認爲這個問題與lab_test_date/created_at/updated_at的比較,因爲在動作執行和期望檢查之間經過了一段時間。

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

after(:all) do 
    Timecop.return 
end 
+0

我嘗試使用「它」應返回實驗室測試結果的一個組「do Timecop.freeze(Time.now) group_arr = Diagnostics.ltr_for_group(」testgroup2「,@user) expect(group_arr).to eq([ labtestresult3.attributes]) Timecop.return end'但這似乎並不奏效。 – aelor 2014-12-02 10:58:20

+0

@aelor我假設,因爲您在凍結操作之前定義了@ labtestresult3和@ user。在任何計算之前移動凍結。 – 2014-12-02 11:06:29

+0

我在之前(:所有)和之後(:所有)添加了代碼,現在它似乎工作。謝謝 ! – aelor 2014-12-02 11:11:30