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,User,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,User,WorkingArea&items=3")
expect(status).to eq 200
end
end
end

运行rspec时得到的错误是

expected: "filter[query]=b&filter[type]=ct:Tag,User,WorkingArea&items=3"
got: "query=be&type=ct%3ATag%2CUser%2CWorkingArea&items=3

你的控制器有两个参数,filter和items。Type不是参数

你从来没有给过滤器一个值。Filter是一个带有键查询和类型的散列参数,但是您还没有建立这个连接。我的理解是spec-api-documentation将从值中推断类型。

parameter :filter
parameter :items
let(:filter) do
{ query: "be", type: "ct:Tag,User,WorkingArea" }
end
let(:items) { "3" }

注意,您不应该直接测试查询字符串,这是一个实现细节。您应该测试查询是否达到预期的效果。

最新更新