如何在 RSpec 测试中将参数发送到 RSwag 模式之外?

How to send parameter outside of the RSwag schema in the RSpec test?

在 Rails 上的 Ruby 中使用 RSwag 和 RSpec,是否可以通过 run_test! 发送未定义为 parameter 的参数]?

示例测试:

# 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

我想在我的 context 中发送 sort[direction]。到目前为止我尝试了什么:

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

无济于事 - 我获得了 HTTP 200 成功,即使通过 Postman 发送的具有 sort[direction] 定义的相同请求以预期的 HTTP 422 不可处理实体响应。

我已经做到了:

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 文件中,这很好。