route_to
匹配器
route_to
匹配器指定请求(动词 + 路径)是否可路由。当指定非标准 RESTful 路由的路由时,它最有用。
expect(get("/")).to route_to("welcome#index") # new in 2.6.0
或
expect(:get => "/").to route_to(:controller => "welcome")
使用快捷方式语法传递路由规范
给定一个名为“spec/routing/widgetsroutingspec.rb”的文件,其中包含
require "rails_helper"
RSpec.describe "routes for Widgets", type: :routing do
it "routes /widgets to the widgets controller" do
expect(get("/widgets")).
to route_to("widgets#index")
end
end
当我运行 rspec spec/routing/widgets_routing_spec.rb
那么这些示例应该全部通过。
使用详细语法传递路由规范
给定一个名为“spec/routing/widgetsroutingspec.rb”的文件,其中包含
require "rails_helper"
RSpec.describe "routes for Widgets", type: :routing do
it "routes /widgets to the widgets controller" do
expect(:get => "/widgets").
to route_to(:controller => "widgets", :action => "index")
end
end
当我运行 rspec spec/routing/widgets_routing_spec.rb
那么这些示例应该全部通过。
不存在的路由的路由规范(失败)
给定一个名为“spec/routing/widgetsroutingspec.rb”的文件,其中包含
require "rails_helper"
RSpec.describe "routes for Widgets", type: :routing do
it "routes /widgets/foo to the /foo action" do
expect(get("/widgets/foo")).to route_to("widgets#foo")
end
end
当我运行 rspec spec/routing/widgets_routing_spec.rb
那么输出应该包含“1 个失败”。
使用快捷方式说明符的命名空间路由的路由规范
给定一个名为“spec/routing/adminroutingspec.rb”的文件,其中包含
require "rails_helper"
RSpec.describe "routes for Widgets", type: :routing do
it "routes /admin/accounts to the admin/accounts controller" do
expect(get("/admin/accounts")).
to route_to("admin/accounts#index")
end
end
当我运行 rspec spec/routing/admin_routing_spec.rb
那么这些示例应该全部通过。
使用详细说明符的命名空间路由的路由规范
给定一个名为“spec/routing/adminroutingspec.rb”的文件,其中包含
require "rails_helper"
RSpec.describe "routes for Widgets", type: :routing do
it "routes /admin/accounts to the admin/accounts controller" do
expect(get("/admin/accounts")).
to route_to(:controller => "admin/accounts", :action => "index")
end
end
当我运行 rspec spec/routing/admin_routing_spec.rb
那么这些示例应该全部通过。