我有一个Rails应用程序,可以在其路由上安装另一个引擎,并且还会覆盖引擎中的某些路由。这是routes.rb:
Spree::Core::Engine.routes.draw do
# some other routes
root :to => "home#index"
end
MyNamespace::Application.routes.draw do
class CityConstraint
def matches?(request)
Spree::CityZone.where(:url => request.params[:city_name]).exists?
end
end
mount Spree::Core::Engine, :at => ':city_name/', :constraints => CityConstraint.new, :as => :city
mount Spree::Core::Engine, :at => '/'
end
当我尝试使用RSpec(2.14)测试路线时,我总是会收到以下错误:
#encoding: utf-8
require 'spec_helper'
RSpec.describe "routes.rb" do
it "test routing" do
expect(get: "/").to route_to(controller: "spree/home", action: "index")
end
end
Failure/Error: expect(get: "/").to route_to(controller: "home", action: "index") No route matches "/" # ./spec/routing/routes_spec.rb:6:in `block (2 levels) in <top (required)>'
我发现,当我添加以下行时,它可以工作:
RSpec.describe "routes.rb" do
routes { Spree::Core::Engine.routes } # this sets the routes
it "test routing" do
expect(get: "/").to route_to(controller: "spree/home", action: "index")
end
end
问题是,我想测试整个应用,因为我们在城市名称范围(例如/your_city
)和根/
下安装了应用两次。
当我尝试在测试中设置routes { MyNamespace::Application.routes }
时,我收到No route matches "/"
错误。
有关如何测试整个已安装路线堆栈的想法,包括来自引擎的路线?
答案 0 :(得分:0)
您可以尝试手动添加所需的路线:
RSpec.describe "routes.rb" do
before :all do
engine_routes = Proc.new do
mount Spree::Core::Engine,
:at => ':city_name/',
:constraints => CityConstraint.new,
:as => :city
mount Spree::Core::Engine, :at => '/'
end
Rails.application.routes.send :eval_block, engine_routes
end
it "test routing" do
expect(get: "/").to route_to(controller: "spree/home", action: "index")
end
end
意见取自:http://makandracards.com/makandra/18761-rails-3-4-how-to-add-routes-for-specs-only