如何在 Rails 3.1 可安装引擎中测试路由 [英] How to test routes in a Rails 3.1 mountable engine
问题描述
我正在尝试为可安装的 Rails 3.1 引擎编写一些路由规范.我有工作模型和控制器规范,但我不知道如何指定路由.
对于示例引擎testy",我尝试的每种方法都以相同的错误结束:
<块引用> ActionController::RoutingError:没有路由匹配/testy"
我尝试过 Rspec 和 Test::Unit 语法(spec/routing/index_routing_spec.rb):
描述测试控制器路由"做它将根路由到测试控制器的索引操作"做{ :get =>'/testy/' }.should route_to(:controller => 'test', :action => 'index')结尾它使用 Test::Unit 语法尝试相同的事情"做assert_routing({:method => :get, :path => '/testy/', :use_route => :testy}, {:controller => 'test', :action => 'index'})结尾结尾
我已经正确地布置了路由(config/routes.rb):
Testy::Engine.routes.draw 做根:到 =>'测试#索引'结尾
并将它们安装在虚拟应用程序 (spec/dummy/config/routes.rb) 中:
Rails.application.routes.draw 做安装 Testy::Engine =>"/testy"结尾
并且运行 rails server
并请求 http://localhost:3000/testy/
工作得很好.
我是否遗漏了任何明显的东西,或者这是否还没有正确地融入框架?
更新:正如@andrerobot 所指出的,rspec 人员已在 2.14 版中解决了此问题,因此我相应地更改了我接受的答案.
从 RSpec 2.14 开始,您可以使用以下内容:
描述测试控制器路由"做路线 { Testy::Engine.routes }# ...结尾
来源:https://github.com/rspec/rspec-rails/pull/668
I am trying to write some routing specs for a mountable rails 3.1 engine. I have working model and controller specs, but I cannot figure out how to specify routes.
For a sample engine, 'testy', every approach I try ends with the same error:
ActionController::RoutingError: No route matches "/testy"
I've tried both Rspec and Test::Unit syntax (spec/routing/index_routing_spec.rb):
describe "test controller routing" do
it "Routs the root to the test controller's index action" do
{ :get => '/testy/' }.should route_to(:controller => 'test', :action => 'index')
end
it "tries the same thing using Test::Unit syntax" do
assert_routing({:method => :get, :path => '/testy/', :use_route => :testy}, {:controller => 'test', :action => 'index'})
end
end
I've laid out the routes correctly (config/routes.rb):
Testy::Engine.routes.draw do
root :to => 'test#index'
end
And mounted them in the dummy app (spec/dummy/config/routes.rb):
Rails.application.routes.draw do
mount Testy::Engine => "/testy"
end
And running rails server
and requesting http://localhost:3000/testy/
works just fine.
Am I missing anything obvious, or is this just not properly baked into the framework yet?
Update: As @andrerobot points out, the rspec folks have fixed this issue in version 2.14, so I've changed my accepted answer accordingly.
Since RSpec 2.14 you can use the following:
describe "test controller routing" do
routes { Testy::Engine.routes }
# ...
end
Source: https://github.com/rspec/rspec-rails/pull/668
这篇关于如何在 Rails 3.1 可安装引擎中测试路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!