命名空间控制器规范出现问题

时间:2019-02-26 21:12:17

标签: ruby-on-rails

我有以下api路由:

@app.callback(
    Output("selected-rows1", "data"),
    [Input("my-date-picker-single1", "date")],
    [State("selected-rows1", "data")])
def display_output_1(value, storage):
    if value is not None:
        return {"selected_rows1": df[df["dd"].str.contains(value)].to_json()}

@app.callback(
    Output("selected-rows2", "data"),
    [Input("my-date-picker-single1", "date")],
    [State("selected-rows2", "data")])
def display_output_2(value, storage):
    if value is not None:
        return {"selected_rows2": df[df["dd"].str.contains(value)].to_json()}

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

确认机场路线
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

我可以访问class Api::AirportsController < ApplicationController def index render json: Airport.all end def show @airport = Airport.find(params[:id]) render json: @airport end end 并获得我期望的JSON响应。

但是,我的规范找不到该动作:

http://localhost:3000/api/airports/2555
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>' 的情况(因此行仅是id),但这会出现此错误:

get :show

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您应将其放在params中,如下所示:

get :show, params: { id: 2555 }