使用 RSpec 2 测试 Rails 3.1 引擎路线

发布于 2024-12-05 19:21:38 字数 1738 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少女情怀诗 2024-12-12 19:21:38

尝试更改您的 get 方法以使用特定路由

get("/foo/bars", :use_route => :foo)

Try changing your get method to use the specific route

get("/foo/bars", :use_route => :foo)
暗喜 2024-12-12 19:21:38

好的,我成功了!并写了一篇关于它的博客文章。

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.

攒眉千度 2024-12-12 19:21:38

实际上让这个发挥作用并不难。这有点hacky,因为您将代码添加到引擎的routes.rb 文件中,该文件根据运行的环境而变化。如果您使用的是spec/dummy 站点方法 用于测试引擎,然后在 /config/routes.rb 文件中使用以下代码片段:

# <your_engine>/config/routes.rb

if Rails.env.test? && Rails.application.class.name.to_s == 'Dummy::Application'
 application = Rails.application
else
 application = YourEngine::Engine
end

application.routes.draw do
 ...
end

这基本上所做的是将引擎切换为虚拟应用程序并在测试模式下写入其路由。

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:

# <your_engine>/config/routes.rb

if Rails.env.test? && Rails.application.class.name.to_s == 'Dummy::Application'
 application = Rails.application
else
 application = YourEngine::Engine
end

application.routes.draw do
 ...
end

What this is basically doing is switching out your engine for the dummy app and writing the routes to it when in test mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文