2

我正在尝试使用 RSpec 2 测试 Rails 3.1 引擎。经过大量试验和错误(以及文档和堆栈溢出搜索)后,该应用程序正在运行,并且我已经通过了大部分规范。问题是我的路线规格仍然失败。

对于具有隔离命名空间和控制器 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)>'

有任何想法吗?

4

4 回答 4

1

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

get("/foo/bars", :use_route => :foo)
于 2011-09-21T20:18:14.317 回答
1

好的,我得到了这个工作!并写了一篇关于它的博客文章。

http://www.matthewratzloff.com/blog/2011/09/21/testing-routes-with-rails-3-1-engines/

它需要一种新方法将引擎路由导入应用程序路由集以进行测试。

编辑:使用命名路由处理发现了一个错误并修复了它。

于 2011-09-21T22:09:37.143 回答
0

让它发挥作用实际上并不难。这有点麻烦,因为您要向引擎的 routes.rb 文件添加代码,该文件会根据运行的环境而变化。如果您使用规范/虚拟站点方法来测试引擎,则在 /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

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

于 2013-04-04T21:45:36.640 回答
0

这是官方 rspec 文档解决方案:

https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs/engine-routes

于 2014-01-11T00:09:56.443 回答