我有以下rswag测试:
response '200', 'response with array of objects' do
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
}
run_test!
end
我的API使用JSON响应:
[
"invalid_key": 1,
"invalid_key": 2
]
换句话说,根据上面的模式,无效的响应。
但是,测试不会 not 失败。这是意外的。
另一方面,如果我的API使用空值数组进行响应:"[null]"
我得到了期望的响应:
Failure/Error: test.run
Rswag::Specs::UnexpectedResponse:
Expected response body to match schema: The property '#/0' of type null did not match the following type: object in schema 19e6c168-1da4-58a6-93fc-a33d9d91233a
因此,我的测试是检查是否存在对象数组,而不是检查嵌套对象中的属性是否符合预期。如何使其同时检查属性?
答案 0 :(得分:0)
我在架构中缺少required
子句:
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
required: ['expected_id'] # <- this was missing
}
如果我的端点生成的响应不包含expected_id
键,则会引发测试错误。