路由规范是否支持重定向路由?[R规格] [英] Do routing specs support redirect routes? [RSpec]
问题描述
在对这个问题进行了相当深入的挖掘之后,我在我对文档的理解和我的结果之间陷入了僵局.
根据https://www.relishapp.com/rspec/rspec-rails/v/2-8/docs/routing-specs/route-to-matcher,我们应该能够写出以下内容:
#rspec-rails (2.8.1)#rspec (>= 1.3.1)#rspec-core (~> 2.8.0)# 路由规范需要spec_helper"描述 BusinessUsersController 做描述路由"做它路由到一些外部网址"做get("/business_users/7/external_url").should route_to("http://www.google.com")结尾结尾结尾# 路由.rbBizeebeeBilling::Application.routes.draw 做资源:business_users 做会员做得到external_url"=>重定向(http://www.google.com")结尾结尾结尾
运行此规范会产生以下结果:失败:
1) BusinessUsersController 路由到一些外部 url失败/错误:assert_routing "/business_users/7/external_url", "http://www.google.com"ActionController::RoutingError:没有路由匹配/business_users/7/external_url"# ./spec/routing/business_users_routing_spec.rb:19:in `block (3 levels) in <top (required)>'
我在任何地方都找不到任何人报告此特定问题.
添加细节:手动测试时路由解析得很好.
路由规范/测试专门测试路由是否映射到特定控制器和操作(可能还有一些参数).
我深入研究了 Rails 和 Journey 的内部结构.RSpec 和 Rails(基本上省略了一些细节)使用 Rails.application.routes.recognize_path
来回答这是否可路由?"的问题
例如:
<前>$ 导轨控制台> Rails.application.routes.recognize_path("/business_users/1", method: "GET")=> {:action=>"show", :controller=>"business_users", :id=>"1"}但是,/business_users/1/external_url
的另一端没有控制器.实际上,为了执行重定向,Rails 创建了一个 ActionDispatch::Routing::Redirect
,这是一个小型 Rack 应用程序.从来没有接触过 Rails 控制器.您基本上是在安装另一个 Rack 应用程序来执行重定向.
要测试重定向,我建议改用请求规范(spec/requests
中的文件).类似的东西:
需要spec_helper"描述外部重定向"做它重定向到 google.com"获取/business_users/1/external_url"response.should redirect_to("http://www.google.com")结尾结束
这会隐式地测试路由,并允许您针对重定向进行测试.
After digging fairly deeply on this issue, I've come to an impasse between my understanding of the documentation and my results.
According to https://www.relishapp.com/rspec/rspec-rails/v/2-8/docs/routing-specs/route-to-matcher, we should be able to write the following:
#rspec-rails (2.8.1)
#rspec (>= 1.3.1)
#rspec-core (~> 2.8.0)
# routing spec
require "spec_helper"
describe BusinessUsersController do
describe "routing" do
it "routes to some external url" do
get("/business_users/7/external_url").should route_to("http://www.google.com")
end
end
end
# routes.rb
BizeebeeBilling::Application.routes.draw do
resources :business_users do
member do
get "external_url" => redirect("http://www.google.com")
end
end
end
Running this spec produces the following results: Failures:
1) BusinessUsersController routing routes to some external url
Failure/Error: assert_routing "/business_users/7/external_url", "http://www.google.com"
ActionController::RoutingError:
No route matches "/business_users/7/external_url"
# ./spec/routing/business_users_routing_spec.rb:19:in `block (3 levels) in <top (required)>'
I have not been able to find anyone reporting this specific issue anywhere.
Added detail: the route is resolved perfectly well when testing manually.
Routing specs/tests specialize in testing whether a route maps to a specific controller and action (and maybe some parameters too).
I dug into the internals of Rails and Journey a bit. RSpec and Rails (basically, some details left out) use Rails.application.routes.recognize_path
to answer the question "is this routable?"
For example:
$ rails console > Rails.application.routes.recognize_path("/business_users/1", method: "GET") => {:action=>"show", :controller=>"business_users", :id=>"1"}
However, there's no controller on the other end of /business_users/1/external_url
. In fact, to perform the redirect, Rails has created an instance of ActionDispatch::Routing::Redirect
, which is a small Rack application. No Rails controller is ever touched. You're basically mounting another Rack application to perform the redirection.
To test the redirect, I recommend using a request spec instead (a file in spec/requests
). Something like:
require "spec_helper"
describe "external redirection" do
it "redirects to google.com" do
get "/business_users/1/external_url"
response.should redirect_to("http://www.google.com")
end
end
This tests the route implicitly, and allows you to test against the redirection.
这篇关于路由规范是否支持重定向路由?[R规格]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!