gpt4 book ai didi

ruby-on-rails - 安装发动机的 RSpec 测试路线

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:50 24 4
gpt4 key购买 nike

我有一个 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 "/" 错误。

有什么想法可以测试整个已安装路由堆栈,包括来自引擎的路由吗?

最佳答案

您可以尝试手动添加所需的路由:

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

关于ruby-on-rails - 安装发动机的 RSpec 测试路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33106033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com