1

parameter使用 Ruby on Rails 中的 RSwag 和 RSpec,是否可以通过?发送未定义为 a 的参数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 Unprocessable Entity 响应。

4

1 回答 1

3

我设法做到了这一点:

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

于 2020-09-15T05:30:50.287 回答