如何在rspec中测试路由错误?

时间:2014-04-15 23:43:37

标签: ruby-on-rails ruby rspec controllers

我通过删除show动作来简化资源,因为它不需要。只需要列出,创建和编辑。我仍然在我的rspecs中进行了SHOW测试,现在它失败了(显然我已经在我的路线文件中添加了:except => [:show]

这就是我失败的原因:

1) CampaignsController GET show assigns the requested campaign as @campaign
     Failure/Error: get :show, {:id => campaign.to_param}, valid_session
     ActionController::RoutingError:
       No route matches {:id=>"458", :controller=>"campaigns", :action=>"show"}
     # ./spec/controllers/campaigns_controller_spec.rb:49:in `block (3 levels) in <top (required)>'

  2) CampaignsController routing routes to #show
     Failure/Error: expect(:get => "/campaigns/1").to route_to("campaigns#show", :id => "1")
       No route matches "/campaigns/1"
     # ./spec/routing/campaigns_routing_spec.rb:15:in `block (3 levels) in <top (required)>'

如何让这些测试通过,以便我期待路由错误?

1 个答案:

答案 0 :(得分:8)

你可以做到

expect{ get :show }.to raise_error(ActionController::RoutingError)

请参阅https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/expect-error

您还可以考虑路由规范:https://www.relishapp.com/rspec/rspec-rails/v/2-4/docs/routing-specs