在我的引擎“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"
但是,福尔摩斯,为什么?