Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request (GET/POST) routes in RSpec that have a wildcard

I have this (admittedly hideous) route in Rails:

scope '/software' do
  post '/:software_id/:attachment_id/event/*event' => 'software#post_event', as: 'post_event'
end

(I would change it but for a legacy API)

And I am writing an RSpec test for it.

rake routes gives me:

post_event POST   /software/:software_id/:attachment_id/event/*event(.:format)     api/version1301/software#post_event

My test looks like this:

  describe "post_event" do

    it "should respond with 204" do
      params = {
        attachment_id: @attachment.uid,
        software_id: @license.id
      }

      post :post_event, params

      response.code.should eq "204"
    end
  end

But I get the 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 do you handle the route with the wildcard (event)?

like image 641
campeterson Avatar asked Apr 17 '13 23:04

campeterson


1 Answers

(Answering my own question)

This turned out to be a bit of a 'rookie' mistake.

The wildcard (event) part of the route still requires a parameter. I wasn't passing the 'event' parameter, therefore the route was incomplete.

The following code works:

describe "post_event" do

  it "should respond with 204" do
    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 a parameter.

Special note to self and others:

If you ever have routing errors in Rails, verify that you're passing all the parameters.

like image 194
campeterson Avatar answered Nov 19 '22 09:11

campeterson