ruby - How to request (GET/POST) routes in RSpec that have a wildcard -
i have (admittedly hideous) route in rails:
scope '/software' post '/:software_id/:attachment_id/event/*event' => 'software#post_event', as: 'post_event' end
(i change legacy api)
and writing rspec test it.
rake routes
gives me:
post_event post /software/:software_id/:attachment_id/event/*event(.:format) api/version1301/software#post_event
my test looks this:
describe "post_event" "should respond 204" params = { attachment_id: @attachment.uid, software_id: @license.id } post :post_event, params response.code.should eq "204" end end
but following routing error:
failure/error: post :post_event, params actioncontroller::routingerror: no route matches {:format=>"json", :name_path=>:api, :attachment=>"7b40ab6a-d522-4a86-b0de-dfb8081e4465", :software_id=>"0000001", :attachment_id=>"7b40ab6a-d522-4a86-b0de-dfb8081e4465", :controller=>"api/version1301/software", :action=>"post_event"} # ./spec/controllers/api/version1301/software_controller_spec.rb:62:in `block (4 levels) in '
how handle route wildcard (event)?
(answering own question)
this turned out bit of 'rookie' mistake.
the wildcard (event) part of route still requires parameter. wasn't passing 'event' parameter, therefore route incomplete.
the following code works:
describe "post_event" "should respond 204" params = { attachment_id: @attachment.uid, software_id: @license.id, event: 'some/event' } post :post_event, params response.code.should eq "204" end end
/*event(.:format)
means it's expecting parameter.
special note self , others:
if ever have routing errors in rails, verify you're passing parameters.
Comments
Post a Comment