Ruby on rails 通配符路由的Rspec路由测试失败

Ruby on rails 通配符路由的Rspec路由测试失败,ruby-on-rails,ruby-on-rails-4,rspec,routing,rspec-rails,Ruby On Rails,Ruby On Rails 4,Rspec,Routing,Rspec Rails,spec/routing/user_spec.rb require 'rails_helper' require 'spec_helper' describe User do it 'should route properly' do expect(get: '/users').to route_to(controller: 'users', action: 'index') expect(get: '/foo/bar').to route_to(con

spec/routing/user_spec.rb

require 'rails_helper'
require 'spec_helper'

describe User do
    it 'should route properly' do
        expect(get: '/users').to route_to(controller: 'users', action: 'index')
        expect(get: '/foo/bar').to route_to(controller: 'users', action: 'index')

    end
end
resources :users

match '*path', to: "users#index", via: :get
config/routes.rb

require 'rails_helper'
require 'spec_helper'

describe User do
    it 'should route properly' do
        expect(get: '/users').to route_to(controller: 'users', action: 'index')
        expect(get: '/foo/bar').to route_to(controller: 'users', action: 'index')

    end
end
resources :users

match '*path', to: "users#index", via: :get
当我使用我的浏览器点击这个url时,这部分工作得非常好

日志

Started GET "/foo/bar" for 127.0.0.1 at 2014-06-12 13:42:39 +0530
Processing by UsersController#index as HTML
  Parameters: {"path"=>"foo/bar"}
  User Load (34.6ms)  SELECT "users".* FROM "users"
  Rendered users/index.html.erb within layouts/application (51.1ms)
Completed 200 OK in 261ms (Views: 222.4ms | ActiveRecord: 36.8ms)
但当我使用以下工具运行测试时:

rspec spec/routing/user_spec.rb
它失败,出现以下错误

Failure/Error: expect(get: '/foo/bar').to route_to(controller: 'users', action: 'index')
       The recognized options <{"controller"=>"users", "action"=>"index", "path"=>"foo/bar"}> did not match <{"controller"=>"users", "action"=>"index"}>, difference:.
       --- expected
       +++ actual
       @@ -1 +1 @@
       -{"controller"=>"users", "action"=>"index"}
       +{"controller"=>"users", "action"=>"index", "path"=>"foo/bar"}
     # ./spec/routing/user_spec.rb:7:in `block (2 levels) in <top (required)>'
Failure/Error:expect(get:'/foo/bar')。将_路由到(控制器:'users',操作:'index'))
识别的选项“用户”、“操作”=>“索引”、“路径”=>“foo/bar”}>与“用户”、“操作”=>“索引”}>不匹配,差异:。
---期望
+++实际的
@@ -1 +1 @@
-{“控制器”=>“用户”,“操作”=>“索引”}
+{“控制器”=>“用户”,“操作”=>“索引”,“路径”=>“foo/bar”}
#./spec/routing/user_spec.rb:7:in'block(2层)in'

我做错了什么?我如何纠正这一点以使测试通过?

Rails将路径(
'foo/bar'
)中与通配符段(
*path
)匹配的部分分配给与该段同名的请求参数,以便操作可以看到客户端是如何到达那里的

您只需将参数添加到测试预期的结果中:

describe UserController do
  describe 'wildcard route'
    it "routes to users#index with path set to the requested path" do
      expect(get: '/foo/bar').to route_to(
        controller: 'users', action: 'index', path: 'foo/bar')
    end
  end
end

是的,在仔细查看日志和错误消息后,我明白了这一点。加上控制台中的
Rails.application.routes.recognize\u path(“/foo/bar”,方法:“GET”)
的结果,我得到了相同的结果。因此,我必须根据名为
path
的参数以及
controller
action
来测试。