Как отправить параметр за пределы схемы RSwag в тесте RSpec?

#ruby-on-rails #ruby #rspec #rspec-rails #rswag

#ruby-on-rails #ruby #rspec #rspec-rails #rswag

Вопрос:

С помощью RSwag и RSpec в Ruby on Rails возможно ли отправить параметр, который не определен как parameter через run_test! ?

Пример теста:

 # frozen_string_literal: true

require "swagger_helper"

describe "Resources", type: :request, swagger_doc: "api/resources.json" do
  path "/resources" do
    get "get resources" do
      tags "resources"

      parameter name: "sort[column]",
                in: :query,
                type: :string,
                enum: ["created_at", "updated_at"]
                required: false
      parameter name: "sort[order]",
                in: :query,
                type: :string,
                enum: ["asc", "desc"]
                required: false

      response "422", "Unprocessable Entity" do
        context "with unrecognized param" do
          # define sort[direction] here

          run_test! do |respone|
            expect(json_response["errors"]).to contain_exactly(
              # my serialized response error
            )
          end
        end
      end
    end
  end
end
  

Я хотел бы отправить sort[direction] в моем context . Что я пробовал до сих пор:

 let(:"sort[direction]") { "asc" }
let(:sort) { { direction: "asc" } }
let(:sort) { { "direction" => "asc" } }
  

безрезультатно — я получил успешный HTTP 200, хотя тот же запрос, отправленный через Postman с sort[direction] определенным, отвечает ожидаемым HTTP 422 необработанным объектом.

Ответ №1:

Мне удалось сделать это таким образом:

 context "with unrecognized sort param" do
  before do |example|
    example.metadata[:example_group][:operation][:parameters]  = [{
      name: "sort[direction]",
      in: :query,
      type: :string,
      required: false
    }]
  end

  let(:"sort[direction]") { "asc" }

  run_test! do |respone|
    expect(json_response["errors"]).to contain_exactly(
    # my serialized response error
    )
  end
end
  

Это не добавляет параметры в сгенерированный файл OpenAPI JSON, что хорошо.

Комментарии:

1. или parameter name: :'sort[direction]', in: :query, type: :string, required: false