这是我正在测试的代码:
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'}
然而我收到的错误是'locations#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
有什么原因吗?
答案 0 :(得分:1)
&#39;显示&#39;行动需要&#39; id&#39; PARAM
location GET /locations/:id(.:format) locations#show
尝试类似
的内容it{ should redirect_to controller: 'locations', action: 'show', id: <yourLocationObj>.id }
或者
it{ should redirect_to(assigns(:location)) }