简体   繁体   中英

How to use rails 'get' in a rspec test

Inside a controller spec I have the following code...

    subject do
      get 'index'
      puts 'response is ' + response.body
      JSON.parse(response.body)
    end

This works fine, getting the index action for the specific api controller. However when I try a show request, I don't know how to pass in a variable, so when I run...

    subject do
      get 'show'
      puts 'response is ' + response.body
      JSON.parse(response.body)
    end

I get the error...

 ←[31mFailure/Error:←[0m ←[31mget 'show'←[0m
 ←[31mActionController::RoutingError←[0m:
   ←[31mNo route matches {:controller=>"api/example/v1/clinics", :action=>"show"}←[0m

There is a show action for the api/example/v1/clinics, of course I didn't pass the parameter which it needs, unsure how to do that. How would I get a show action working?

I tried something like

    get 'api/example/v1/clinics/2'

but that does not work either. I can't seem to locate the documentation for this rails method either. Any help is appreciated. Thanks.

您可以传入一个ID以get :show by:

get 'show', id: 2

For controller actions that require parameters (for example 'id' is required by the show action) you need to pass it as a parameter to the get request:

get :show, id: 1

Or if using older 1.8 hash syntax:

get :show, :id => 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM