2012-07-20 48 views
8

我已经在the normal fashion中创建了一个Rails引擎,安装了RSpec,并为一个模型生成了一个脚手架,但是我无法获得任何路由规范可以通过。所有路由示例都失败了Rails 3.2引擎RSpec 2.10

这里有一个例子:

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 

发动机能正常安装在虚拟应用程序:

# spec/dummy/config/routes.rb 
Rails.application.routes.draw do 
    mount Licensing::Engine => "/licensing" 
end 

我可以进入虚拟应用程序并启动控制台并获取该路线:

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>..." 

虚拟应用程序和RSpec之间存在一些差异,我无法弄清楚它是什么。我发现几篇文章,声称能解决这个问题,但他们都没有帮助,而且其中有几个具体到Rails 3.1:

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

+0

我假设你对一个虚拟应用程序,能够将您的发动机测试。你确定引擎是安装在虚拟应用程序的路线上吗?如果是的话,你确定你的规范是':type =>:routing'? – 2012-08-02 03:36:51

+0

@TanzeebKhalili我确定路由是正确的,因为他们在控制台和浏览器中工作。我从虚拟应用内运行规格。这些文件在'spec/routing'下,并且明确指定':type =>:routing'不会导致这些例子通过。还有什么想法? : -/ – Brandan 2012-08-02 15:39:09

+1

以下更改如何:1)描述的不同对象:“描述”路径“do ...”和2)不同的语法:'{:get =>'/ licensing/licenses'} .should be_routable' – 2012-08-03 07:17:51

回答

10

更新(2013- 10-31)

RSpec 2.14 now fully supports engine routes

module MyEngine 
    describe ExampleController do 
    routes { MyEngine::Engine.routes } 
    end 
end 

将它添加到所有路由规格:

# 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是否能够在特定路径上安装引擎。我只测试我的引擎已经正确设置了自己的路线。不过,我宁愿不必重写一个实例变量来实现它。

下面是有关这个主题的另一对夫妇良好的线程:

+0

第二个链接为我解决了这个问题。非常感谢! – 2012-12-06 13:00:17

+0

此解决方案在rspec 2.14中停止工作! – 2013-07-12 15:22:59

+0

是的,有 路线{许可:: Engine.routes} 来取代它:你可以将它添加到一个规范的,但我只需要找到一个解决方案,以增加这__all__发动机控制器规格... – mugwump 2013-11-15 10:07:28

7

我想你需要明确地通知RSpec你正在使用命名空间Rails路由。 EG,

# :use_route => ENGINE_NAMESPACE 
:use_route => 'licensing' 

此外,为什么不简化的RSpec的控制器测试这样的书面方式,

describe Licensing::LicensesController do 
    it 'GET index' do 
    get :index, use_route: 'licensing' 
    end 
end 

下面是RSpec的控制器我做了一个链接, https://github.com/westonplatter/questionnaire_engine/tree/engine/spec/controllers

+0

感谢您的回答,但我的问题是特别是关于路由测试 - 也就是说,我想明确测试'/ licensing/licenses'路由到正确的控制器和操作,而不仅仅是我可以在给定的控制器上得到'index'。我想我可能找到了一个解决方案,尽管这不太好。 – Brandan 2012-08-11 14:56:15

相关问题