2013-06-28 117 views
3

在我的引擎'Utilizer'中,我试图用Rspec 3.2测试路由。 命名空间被隔离无法在Rails 3.2下测试引擎路由+ Rspec 2.13

# lib/utilizer/engine.rb 
module Utilizer 
    class Engine < ::Rails::Engine 
     isolate_namespace Utilizer 
     ... 
    end 
end 

发动机被安装到虚设应用程式:

# spec/dummy/config/routes.rb 
    Rails.application.routes.draw do 
     mount Utilizer::Engine => "/utilizer", as: 'utilizer' 
    end 

要我已经添加了几个提供配置的作为(来自here)以下spec_helper.rb:

# spec/spec_helper.rb 
RSpec.configure do |config| 
    ... 
    config.before(:each, type: :routing) { @routes = Utilizer::Engine.routes } 
    ... 
end 

当我定义路线时:

# config/routes.rb 
Utilizer::Engine.routes.draw do 
    resources :auths, id: /\d+/, only: [:destroy] 
end 

瑞克显示它适当的虚设应用程式:

$ spec/dummy > bundle exec rake routes 

$ utilizer /utilizer Utilizer::Engine 
$ Routes for Utilizer::Engine: 
$ auth DELETE /auths/:id(.:format) utilizer/auths#destroy {:id=>/\d+/} 

但两者Rspec的测试

# spec/routing/auths_routing_spec.rb 
require 'spec_helper' 

describe "Auths page routing" do 

    let!(:auth) { create(:utilizer_auth) } # factory is working properly by itself 

    describe "#destroy" do 
    let!(:action) { { controller: "utilizer/auths", action: "destroy", id: auth.id } } 
    specify { { delete: "/auths/#{ auth.id }" }.should route_to(action) } 
    specify { { delete: auth_path(auth) }.should route_to(action) } 
    end 
end 

失败,错误(用于第一和第二试验相应):

No route matches "/auths/1" 
No route matches "/utilizer/auths/1" 

但是,福尔摩斯,为什么?

回答

0

我已经在this discussion on Github(感谢Brandan)的末尾提供了Exoth评论中的解决方案。

在我spec_helper而不是

config.before(:each, type: :routing) { @routes = Utilizer::Engine.routes } 

我用

config.before(:each, type: :routing) do 
    @routes = Utilizer::Engine.routes 
    assertion_instance.instance_variable_set(:@routes, Utilizer::Engine.routes) 
end 

和它的作品。