开发者

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 i开发者_开发问答n rspec? (The XXX is hard-coded in the routes.rb file.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜