问题描述
使用Ruby on Rails中的RSwag和RSpec,是否可以通过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]",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" } }
无济于事-即使定义为sort[direction]
的通过邮递员发送的同一请求以预期的HTTP 422不可处理实体响应,我也获得了HTTP 200成功。
解决方法
我设法做到了:
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文件中,这很好。