Ruby on rails 3.2 具有RSpec 2.10的Rails 3.2引擎的所有布线示例均失败

Ruby on rails 3.2 具有RSpec 2.10的Rails 3.2引擎的所有布线示例均失败,ruby-on-rails-3.2,rspec2,rspec-rails,rails-engines,Ruby On Rails 3.2,Rspec2,Rspec Rails,Rails Engines,我在中创建了一个Rails引擎,安装了RSpec,并为模型生成了一个脚手架,但我无法通过任何路由规范 这里有一个例子: describe Licensing::LicensesController do it 'routes to #index' do get('/licensing/licenses').should route_to('licensing/licenses#index') end end 我在虚拟应用程序中运行的示例如下: $ cd spec/dummy $

我在中创建了一个Rails引擎,安装了RSpec,并为模型生成了一个脚手架,但我无法通过任何路由规范

这里有一个例子:

describe Licensing::LicensesController do
  it 'routes to #index' do
    get('/licensing/licenses').should route_to('licensing/licenses#index')
  end
end
我在虚拟应用程序中运行的示例如下:

$ cd spec/dummy
$ rake spec
/Users/brandan/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec ../routing/licensing/licenses_routing_spec.rb
F

Failures:

  1) Licensing::LicensesController routes to #index
     Failure/Error: get('/licensing/licenses').should route_to('licensing/licenses#index')
       No route matches "/licensing/licenses"
     # /Users/brandan/repos/licensing/spec/routing/licensing/licenses_routing_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.04345 seconds
1 example, 1 failure
我可以进入虚拟应用程序,启动控制台,然后就可以很好地找到那条路线:

1.9.3p194 :001 > app.get('/licensing/licenses')
  Licensing::License Load (0.3ms)  SELECT "licensing_licenses".* FROM "licensing_licenses" 
200
1.9.3p194 :002 > app.response.body
"<!DOCTYPE html>..."
1.9.3p194:001>app.get(“/licensing/licenses”)
授权::许可证加载(0.3ms)从“授权许可证”中选择“授权许可证”。*
200
1.9.3p194:002>app.response.body
"..."
虚拟应用程序和RSpec之间存在一些差异,我无法理解它是什么。我发现了几篇文章声称可以解决这个问题,但都没有起到任何作用,其中有几篇是Rails 3.1特有的:


有人在Rails 3.2/RSpec 2.10中解决了这个问题吗?

我认为您需要明确地通知RSpec您正在使用一个名称空间的Rails路由。例如

# :use_route => ENGINE_NAMESPACE
:use_route => 'licensing'
此外,为什么不简化RSpec控制器测试的编写,如下所示:

describe Licensing::LicensesController do
  it 'GET index' do
    get :index, use_route: 'licensing'
  end
end
这是我做的RSpec控制器的链接,

更新(2013-10-31)

RSpec 2.14现在:

将其添加到所有布线规范:

# spec/spec_helper.rb
config.include Module.new {
  def self.included(base)
    base.routes { Reportr::Engine.routes }
  end 
}, type: :routing

到目前为止,我能找到的最佳解决方案是显式设置测试期间将使用的路由集:

RSpec.configure do |config|
  config.before(:each, type: :controller) { @routes = Licensing::Engine.routes }
  config.before(:each, type: :routing)    { @routes = Licensing::Engine.routes }
end
然后,我不得不重写路由示例以省略名称空间:

it 'routes to #index' do
  get('/licenses').should route_to('licensing/licenses#index')
end
这看起来有点像黑客,但确实有道理。我不是在测试Rails是否能够在特定路径上安装引擎。我只是测试我的引擎是否正确设置了自己的路线。尽管如此,我还是不想覆盖实例变量来实现它

以下是关于此主题的另外两个好帖子:


我假设您正在测试一个集成您的引擎的虚拟应用程序。你确定引擎安装在虚拟应用程序的路径上吗?如果是,您确定您的规范是
:type=>:routing
?@tanzeebkhali我确信这些路由是正确的,因为它们在控制台和浏览器中工作。我正在虚拟应用程序中运行规格。文件位于
spec/routing
下,明确指定
:type=>:routing
也不会导致示例通过。还有什么想法吗-/以下更改如何:1)不同的描述对象:
描述“路由”执行…
和2)不同的语法:
{:get=>'/licensing/licenses'}。应该是可路由的
@TanzeebKhalili抱歉,这两个都没有帮助。我发现这似乎表明这是Rails的问题,而不是RSpec的问题,但仍然没有解决方案。谢谢你的回答,但我的问题是关于路由测试的,也就是说,我想明确测试
/licensing/licenses
路由到正确的控制器和操作,不仅仅是我可以在给定的控制器上
get:index
。我想我可能已经找到了一个解决方案,尽管它并不漂亮。是的,还有路由{Licensing::Engine.routes}来替代它:您可以将它添加到规范中,但我只需要找到一个解决方案,将其添加到引擎的所有控制器规格中…@mugwump我添加了一个示例,演示如何在
spec\u helper.rb
@Brandan谢谢!工作和帮助干燥我的规格!
RSpec.configure do |config|
  config.before(:each, type: :controller) { @routes = Licensing::Engine.routes }
  config.before(:each, type: :routing)    { @routes = Licensing::Engine.routes }
end
it 'routes to #index' do
  get('/licenses').should route_to('licensing/licenses#index')
end