Ruby on rails 使用rspec测试自定义路由错误处理
我有一个rails 4 api应用程序,其中捕获了所有丢失的路由,如下所示:Ruby on rails 使用rspec测试自定义路由错误处理,ruby-on-rails,ruby-on-rails-4,rspec,Ruby On Rails,Ruby On Rails 4,Rspec,我有一个rails 4 api应用程序,其中捕获了所有丢失的路由,如下所示: namespace :api, defaults: {format: :json} do namespace :v1 do # matchers ... end # catch all undefined paths and redirect them to the errors controller match "*path", to: "errors#routing_error", via:
namespace :api, defaults: {format: :json} do
namespace :v1 do
# matchers ...
end
# catch all undefined paths and redirect them to the errors controller
match "*path", to: "errors#routing_error", via: :all
end
通过一个简单的点击测试,这似乎像一个魅力,但我想写
此行为的rspec测试
在我的Rspec规范中,我尝试了以下方法:
it "should capture non existing action" do
get '/api/non-existing-action', format: :json #Note that non-existing-action is not defined in the routes.rb
#expectations come here
end
问题是Rspec似乎在路由器之前捕捉到路由错误,并引发类似这样的默认错误
ActionController::UrlGenerationError:
没有路由匹配{:action=>“/api/non-existing action”,:controller=>“api/api”,:format=>:json}
所以我的问题是:有没有办法告诉Rspec它应该让我的路由器处理不存在的路径,这样我就可以从我的错误控制器那里得到错误响应?你试过matcher吗?它仍然会出错吗?谢谢,但我得到了完全相同的错误。你发现了吗?不,不幸的是没有