2014-09-26 47 views
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结果还是有更好的方法?

回答

2

我只钻入返回的哈希值并对其进行验证。

expect(assigns(@step)["step"]).to include(@test_step_item) 

编辑

expect(assigns(@step)["step"]).to eq(@test_step_item) 

我看了你原来失败的消息是错误的。包括是不正确的匹配器。 assigns(@step)["step"]应该会给你一个完全匹配@test_step_item

+0

谢谢jdenen。这似乎是朝着正确方向迈出的一步。当我将代码替换为代码片段时,测试仍然失败,并且包含“标记为相同原点”的部分。我还可以在这里抽出正确的k,v对吗? – 2014-09-26 15:31:49

+0

FWIW,diff部分更小,如下所示:@@ -1,2 +1,2 @@ – 2014-09-26 15:34:53

+0

您可以发布整个失败消息吗? – Johnson 2014-09-26 15:37:18