Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call a specific url with rspec

I want to create a get request in rspec.

    get :exec, {:query=>"bla",
      :id => "something",
      :user_id => "user"
    }

This builds a URL like: /user/query/something/exec?query=bla

The thing is that my controller checks the request it gets and the url must look like: /user/query/something/_XXX_/exec?query=bla

How can I do something like this in rspec? (The XXX is hard-coded in the routes.rb file.)

like image 580
Stephan Avatar asked Dec 16 '10 15:12

Stephan


1 Answers

I'm assuming you're referring to a controller spec.

When you pass in a hash like in your example, the keys will be matched against the variables in your routes. For any key that doesn't match the route, the key/value pair will be appended as a query string.

For example, suppose you have this in your spec:

get :exec, :query => 'foo', :id => '1', :user_id => 42

And you have this in your routes (Rails 3 style):

match '/exec/:user_id/:id' => 'whatever#exec'

The spec will then substitute in the key/value pairs you've given and simulate a request with the following path:

/exec/42/1?query=foo

So, to wire up your specs to your routes, just make sure you're properly matching the variable names in your routes to the params in your spec request.

like image 133
rlkw1024 Avatar answered Sep 28 '22 03:09

rlkw1024