我有一个我认为应该通过的测试。最后一行看起来像这样:
期望(指定(@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转换为字符串,修改该字符串,然后针对expect(assigns(@step))测试它。结果还是有更好的方法?
答案 0 :(得分:2)
我只是深入查看返回的Hash并对其进行验证。
expect(assigns(@step)["step"]).to include(@test_step_item)
修改强>
expect(assigns(@step)["step"]).to eq(@test_step_item)
我读错了你原来的失败信息。包括不是正确的匹配器。 assigns(@step)["step"]
应该与@test_step_item
完全匹配。