0

I have these api routes:

  namespace :api do
    resources :deals, :users
    :airports, only: [:index, :show]
  end

I confirm the airport routes with rake routes -g airports

      Prefix Verb URI Pattern                 Controller#Action
 api_airports GET  /api/airports(.:format)     api/airports#index
 api_airport GET  /api/airports/:id(.:format) api/airports#show

Here's the controller:

class Api::AirportsController < ApplicationController
  def index
    render json: Airport.all
  end

  def show
    @airport = Airport.find(params[:id])
    render json: @airport
  end
end

And I can visit http://localhost:3000/api/airports/2555 and get the JSON response I expect.

However, my specs can't find the action:

describe Api::AirportsController, type: :controller do
  describe "show" do
    it "returns a JSON:API-compliant, serialized object representing the specified Airport" do
      correct_hash = {
        "id" => "2555",
        "type" => "airports",
        "attributes" => {
          "name" => "Ronald Reagan Washington National Airport",
          "city" => "Washington",
          "country" => "United States",
          "iata" => "DCA" 
        },
        "jsonapi" => {
          "version" => "1.0"
        }
      }

      get :show, id: 2555
      returned_json = response.body
      parsed_json = JSON.parse(returned_json)
      expect(parsed_json).to eq correct_hash
    end
  end
end
Failures:

  1) Api::AirportsController show returns a JSON:API-compliant, serialized object representing the specified Airport
     Failure/Error: get :show, id: 2555

     ArgumentError:
       unknown keyword: id
     # ./spec/api/airport_api_spec.rb:18:in `block (3 levels) in <main>'

I've tried it without id (so the line is just get :show) but that gives this error:

Failures:

  1) Api::AirportsController show returns a JSON:API-compliant, serialized object representing the specified Airport
     Failure/Error: get :show

     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"api/airports"}
     # ./spec/api/airport_api_spec.rb:20:in `block (3 levels) in <main>'

What am I doing wrong?

Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129

1 Answers1

1

You should put this in params, like this:

get :show, params: { id: 2555 }
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91