0
我有一個我認爲應該通過的測試。最後一行是這樣的:我如何在RSpec中爲我的控制器測試匹配這個結果
預期(受讓人(@step))包括@test_step_item
測試失敗並輸出:
1) StepsController assigns the requested StepItem to @step
Failure/Error: expect(assigns(@step)).to include @test_step_item
expected {"marked_for_same_origin_verification" => true, "step" => #<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 14:57:11", updated_at: "2014-09-26 14:57:11", orientation_id: 1, sequence_id: 1>, "type" => "Step", "_optimized_routes" => true, "current_user" => nil}
to include #<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 14:57:11", updated_at: "2014-09-26 14:57:11", orientation_id: 1, sequence_id: 1>
Diff:
@@ -1,2 +1,6 @@
-[#<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 14:57:11", updated_at: "2014-09-26 14:57:11", orientation_id: 1, sequence_id: 1>]
+"_optimized_routes" => true,
+"current_user" => nil,
+"marked_for_same_origin_verification" => true,
+"step" => #<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 14:57:11", updated_at: "2014-09-26 14:57:11", orientation_id: 1, sequence_id: 1>,
+"type" => "Step",
# ./spec/controllers/steps_controller_spec.rb:29:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
...這看起來像是很接近我想要的:
{"marked_for_same_origin_verification" => true, "step" =>
預設爲預期的散列值。
我已經嘗試過幾個RSpec匹配器,比如'include'和'match',但是這些不會通過。我試着用end_with這樣的:
expect(assigns(@step)).to end_with @test_step_item
,但仍看到這一點:
1) StepsController assigns the requested StepItem to @step
Failure/Error: expect(assigns(@step)).to end_with @test_step_item
expected {"marked_for_same_origin_verification"=>true, "step"=>#<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 15:03:02", updated_at: "2014-09-26 15:03:02", orientation_id: 1, sequence_id: 1>, "type"=>"Step", "_optimized_routes"=>true, "current_user"=>nil}
to end with #<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 15:03:02", updated_at: "2014-09-26 15:03:02", orientation_id: 1, sequence_id: 1>
# ./spec/controllers/steps_controller_spec.rb:28:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
附錄
試圖jdenen的建議後,事情看起來更近,但還是不及格。這:
expect(assigns(@step)["step"]).to include(@test_step_item)
產生這樣的:
1) StepsController assigns the requested StepItem to @step
Failure/Error: expect(assigns(@step)["step"]).to include(@test_step_item)
expected #<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 15:34:09", updated_at: "2014-09-26 15:34:09", orientation_id: 1, sequence_id: 1> to include #<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 15:34:09", updated_at: "2014-09-26 15:34:09", orientation_id: 1, sequence_id: 1>, but it does not respond to `include?`
Diff:
@@ -1,2 +1,2 @@
-[#<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 15:34:09", updated_at: "2014-09-26 15:34:09", orientation_id: 1, sequence_id: 1>]
+#<StepItem id: 1, type: "StepItem", note: "Could have a note", position: 1, name: nil, institution_id: 1, protocol_id: nil, created_at: "2014-09-26 15:34:09", updated_at: "2014-09-26 15:34:09", orientation_id: 1, sequence_id: 1>
我應該對象@test_step_item轉換爲字符串,修改字符串,然後測試它反對期待(受讓人(@step)) .to結果還是有更好的方法?
謝謝jdenen。這似乎是朝着正確方向邁出的一步。當我將代碼替換爲代碼片段時,測試仍然失敗,並且包含「標記爲相同原點」的部分。我還可以在這裏抽出正確的k,v對嗎? – 2014-09-26 15:31:49
FWIW,diff部分更小,如下所示:@@ -1,2 +1,2 @@ – 2014-09-26 15:34:53
您可以發佈整個失敗消息嗎? – Johnson 2014-09-26 15:37:18