无法在Rails 3.2 + Rspec 2.13下测试引擎路由

时间:2013-06-28 12:39:39

标签: ruby-on-rails-3 rspec routing rspec-rails rule-engine

在我的引擎'Utilizer'中,我正试图用Rspec 3.2测试路线。 名称空间是孤立的

# 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

到spec_helper.rb我添加了几个配置如下(来自here):

# 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

Rake为虚拟应用程序正确显示:

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

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

2 个答案:

答案 0 :(得分:8)

自RSpec 2.14起,您可以使用以下内容:

describe "Auths page routing" do
  routes { Utilizer::Engine.routes }
  # ...
end

来源:https://github.com/rspec/rspec-rails/pull/668

答案 1 :(得分:0)

我在this discussion on Github结束时在Exoth的评论中找到了解决方案(感谢Brandan)。

在我的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

它有效。