如何解决rspec api 文档的组合查询参数
我正在研究我实现的搜索端点的测试和文档。我无法正确添加查询参数。基本上请求 url 应该是这样的
"/api/v3/workspaces/1/searches?filter[query]=b&filter[type]=ct:Tag,User,WorkingArea"
我的控制器看起来像这样
class SearchesController < ApiV3Controller
load_and_authorize_resource :workspace
load_and_authorize_resource :user,through: :workspace
load_and_authorize_resource :working_area,through: :workspace
load_and_authorize_resource :tag,through: :workspace
def index
@resources = relevant_search_results
render_json(@resources)
end
private
def ability_klasses
[WorkspaceAbility,UserWorkspaceAbility,WorkingAreaAbility,TagAbility]
end
def relevant_search_results
query = filtered_params[:query]
types = filtered_params[:type]
items = params[:items]
GlobalSearcher.new(query,types,items,@workspace).relevant_search_results
end
def render_json(resources)
render json: resources,status: :ok
end
def filtered_params
params.require(:filter).permit(:query,:type)
end
end
resource "Searches",:include_basic_variables,type: :api do
parameter :filter
parameter :type
parameter :items
let(:query) { "be" }
let(:type) { "ct:Tag,WorkingArea" }
let(:items) { "3" }
let_it_be(:workspace_id) { company.id }
explanation "Searches resource"
route "/api/v3/workspaces/:workspace_id/searches","Index" do
with_options with_example: true,required: true do
parameter :workspace_id,"Workspace ID",type: :integer,example: 1
end
get "List all the relevant items" do
context "Authenticated" do
before { sign_in(admin) }
example 'Search results' do
do_request
expect(query_string).to eq("filter[query]=b&filter[type]=ct:Tag,WorkingArea&items=3")
expect(status).to eq 200
end
end
end
我在运行 rspec 时得到的错误是
expected: "filter[query]=b&filter[type]=ct:Tag,WorkingArea&items=3"
got: "query=be&type=ct%3ATag%2CUser%2CWorkingArea&items=3
解决方法
您的控制器需要两个参数,过滤器和项目。类型不是参数。
你从不给过滤器一个值。 filter 是一个带有查询和类型键的散列参数,但您尚未建立该连接。我的理解是 rspec-api-documentation 将根据值推断类型。
parameter :filter
parameter :items
let(:filter) do
{ query: "be",type: "ct:Tag,User,WorkingArea" }
end
let(:items) { "3" }
请注意,您不应直接测试查询字符串,这是一个实现细节。您应该测试查询是否达到预期效果。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。