使用 RSpec 2 测试 Rails 3.1 引擎路线
我正在尝试使用 RSpec 2 测试 Rails 3.1 引擎。经过大量的试验和错误(以及文档和 Stack Overflow 搜索),该应用程序正在运行,并且我已经通过了大部分规范。问题是我的路线规格仍然失败。
对于具有独立命名空间和控制器 Foo::BarsController 的引擎“foo”,我有这样的:
require "spec_helper"
describe Foo::BarsController do
describe "routing" do
it "routes to #index" do
get("/foo/bars").should route_to("bars#index")
end
it "routes to #new" do
get("/foo/bars/new").should route_to("bars#new")
end
end
end
这导致:
1) Foo::BarsController routing routes to #index
Failure/Error: get("/foo/bars").should route_to("bars#index")
ActionController::RoutingError:
No route matches "/foo/bars"
# ./spec/routing/foo/bars_routing_spec.rb:6:in `block (3 levels) in <top (required)>'
2) Foo::BarsController routing routes to #new
Failure/Error: get("/foo/bars/new").should route_to("bars#new")
ActionController::RoutingError:
No route matches "/foo/bars/new"
# ./spec/routing/foo/bars_routing_spec.rb:10:in `block (3 levels) in <top (required)>'
我的规范虚拟应用程序似乎设置正确:
Rails.application.routes.draw do
mount Foo::Engine => "/foo"
end
如果它有助于回答这个问题,我的视图规范也是不工作。这是一个典型的错误:
9) foo/bars/index.html.erb renders a list of bars
Failure/Error: render
ActionView::Template::Error:
undefined local variable or method `new_bar_path' for #<#<Class:0x00000100c14958>:0x0000010464ceb8>
# ./app/views/foo/bars/index.html.erb:3:in `___sers_matt__ites_foo_app_views_foo_bars_index_html_erb___1743631507081160226_2184232780'
# ./spec/views/foo/bars/index.html.erb_spec.rb:12:in `block (2 levels) in <top (required)>'
有什么想法吗?
I'm trying to test a Rails 3.1 engine with RSpec 2. After a lot of trial and error (and documentation and Stack Overflow searching) the app is working and I've gotten most of the specs to pass. The problem is that my route specs are still failing.
For an engine "foo" with an isolated namespace and a controller Foo::BarsController, I have this:
require "spec_helper"
describe Foo::BarsController do
describe "routing" do
it "routes to #index" do
get("/foo/bars").should route_to("bars#index")
end
it "routes to #new" do
get("/foo/bars/new").should route_to("bars#new")
end
end
end
This results in:
1) Foo::BarsController routing routes to #index
Failure/Error: get("/foo/bars").should route_to("bars#index")
ActionController::RoutingError:
No route matches "/foo/bars"
# ./spec/routing/foo/bars_routing_spec.rb:6:in `block (3 levels) in <top (required)>'
2) Foo::BarsController routing routes to #new
Failure/Error: get("/foo/bars/new").should route_to("bars#new")
ActionController::RoutingError:
No route matches "/foo/bars/new"
# ./spec/routing/foo/bars_routing_spec.rb:10:in `block (3 levels) in <top (required)>'
My spec dummy application seems to be set up correctly:
Rails.application.routes.draw do
mount Foo::Engine => "/foo"
end
If it helps to answer this question, my view specs are also not working. Here is a typical error:
9) foo/bars/index.html.erb renders a list of bars
Failure/Error: render
ActionView::Template::Error:
undefined local variable or method `new_bar_path' for #<#<Class:0x00000100c14958>:0x0000010464ceb8>
# ./app/views/foo/bars/index.html.erb:3:in `___sers_matt__ites_foo_app_views_foo_bars_index_html_erb___1743631507081160226_2184232780'
# ./spec/views/foo/bars/index.html.erb_spec.rb:12:in `block (2 levels) in <top (required)>'
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试更改您的
get
方法以使用特定路由Try changing your
get
method to use the specific route好的,我成功了!并写了一篇关于它的博客文章。
http://www.matthewratzloff.com/blog/2011/09/21/testing-routes-with-rails-3-1-engines/" matthewratzloff.com/blog/2011/09/21/testing-routes-with-rails-3-1-engines/
它需要一种新方法将引擎路由导入到为测试设置的应用程序路由。
编辑:发现了命名路由处理的错误并修复了它。
OK, I got this working! And wrote a blog post about it.
http://www.matthewratzloff.com/blog/2011/09/21/testing-routes-with-rails-3-1-engines/
It requires a new method to import engine routes into the application route set for tests.
Edit: Caught a bug with named route handling and fixed it.
实际上让这个发挥作用并不难。这有点hacky,因为您将代码添加到引擎的routes.rb 文件中,该文件根据运行的环境而变化。如果您使用的是spec/dummy 站点方法 用于测试引擎,然后在 /config/routes.rb 文件中使用以下代码片段:
这基本上所做的是将引擎切换为虚拟应用程序并在测试模式下写入其路由。
It's actually not so hard to get this to work. It's a bit hacky since you are adding code to the engine's routes.rb file that changes depending on which env it's running in. If you are using the spec/dummy site approach for testing the engine then use the following code snippet in /config/routes.rb file:
What this is basically doing is switching out your engine for the dummy app and writing the routes to it when in test mode.
这是官方的 rspec 文档解决方案:
https://www .relishapp.com/rspec/rspec-rails/docs/routing-specs/engine-routes
Here is the official rspec documentation solution:
https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs/engine-routes