如何解决在RSpec测试中如何在RSwag模式之外发送参数?
使用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文件中,这很好。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。