call a specific url with rspec

瘦欲@ 提交于 2019-12-21 12:27:39

问题


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.)


回答1:


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.



来源:https://stackoverflow.com/questions/4462688/call-a-specific-url-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!