3

在我的引擎“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 中,我添加了如下几个配置(从这里开始):

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

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

4

2 回答 2

8

从 RSpec 2.14 开始,您可以使用以下内容:

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

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

于 2013-07-04T15:37:36.130 回答
0

在关于 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

它有效。

于 2013-06-30T07:05:27.207 回答