2015-10-13 87 views
2

我有一个Rails应用程序,它将另一个引擎装载到它的路由中,并且还覆盖引擎中的一些路由。这里是routes.rb中:装有引擎的RSpec测试路由

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

你可以尝试手动添加需要的路由:http://makandracards.com/makandra/18761-rails-3-4-how-to-add-routes-for-specs-only

:取自

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 

理念