Rspec 当路由存在时,没有路由匹配错误

Rspec 当路由存在时,没有路由匹配错误,rspec,Rspec,以下是我正在测试的代码: before_action :set_location, only: [:show] def create @location = Location.new( location_parameters ) if @location.save redirect_to location_path(@location.id) else render action: :new, status: 422 end en

以下是我正在测试的代码:

before_action :set_location, only: [:show]
def create
    @location = Location.new( location_parameters )

    if @location.save
        redirect_to location_path(@location.id)
    else
        render action: :new, status: 422
    end
end
以下是我的路线:

resources :locations
以下是期望:

before { request }
subject { response }

it{ should redirect_to controller: 'locations', action: 'show'}
然而,我得到了这样一个错误,即“地点展示”并不存在,即使搜索路线表明它们确实存在:

Failures:

  1) LocationsController#create response with valid request should redirect to [:controller, "locations"] and [:action, "show"]
     Failure/Error: it{ should redirect_to controller: 'locations', action: 'show'}
     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"locations"}
     # ./spec/controllers/locations_controller_spec.rb:16:in `block (5 levels) in <top (required)>'

Finished in 1.5 seconds (files took 3.34 seconds to load)
17 examples, 1 failure, 5 pending

Failed examples:

rspec ./spec/controllers/locations_controller_spec.rb:16 # LocationsController#create response with valid request should redirect to [:controller, "locations"] and [:action, "show"]
starkers@ubuntu:~/Documents/currentTraining/rspec/geo$ rake routes
       Prefix Verb   URI Pattern                   Controller#Action
    locations GET    /locations(.:format)          locations#index
              POST   /locations(.:format)          locations#create
 new_location GET    /locations/new(.:format)      locations#new
edit_location GET    /locations/:id/edit(.:format) locations#edit
     location GET    /locations/:id(.:format)      locations#show
              PATCH  /locations/:id(.:format)      locations#update
              PUT    /locations/:id(.:format)      locations#update
              DELETE /locations/:id(.:format)      locations#destroy
故障:
1) LocationsController#使用有效请求创建响应应重定向到[:controller,“locations”]和[:action,“show”]
失败/错误:它{应该将_重定向到控制器:“位置”,操作:“显示”}
ActionController::UrlGenerationError:
没有路由匹配{:action=>“show”,:controller=>“locations”}
#./spec/controller/locations\u controller\u spec.rb:16:in'block(5层)in'
在1.5秒内完成(加载文件需要3.34秒)
17个示例,1个失败,5个待定
失败的示例:
rspec./spec/controllers/locations_controller_spec.rb:16#locations controller#使用有效请求创建响应应重定向到[:controller,“locations”]和[:action,“show”]
starkers@ubuntu:~/Documents/currentTraining/rspec/geo$rake routes
前缀动词URI模式控制器#操作
位置获取/位置(:格式)位置#索引
发布/位置(:格式)位置#创建
新位置获取/位置/新(:格式)位置新
编辑位置获取/位置/:id/编辑(:格式)位置编辑
位置获取/位置/:id(:格式)位置#显示
补丁/位置/:id(:格式)位置#更新
放置/位置/:id(:格式)位置#更新
删除/位置/:id(:格式)位置#销毁

有什么原因吗?

'show'操作采用'id'参数

location GET    /locations/:id(.:format)      locations#show
试试像这样的东西

it{ should redirect_to controller: 'locations', action: 'show', id: <yourLocationObj>.id }
it{ should redirect_to(assigns(:location)) }