我有以下的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
]换句话说,根据上面的架构,无效的响应。
然而,测试并没有失败。这真是出乎意料。
另一方面,如果我的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因此,我的测试是检查是否有一个对象数组,而不是检查嵌套对象中的属性是否与预期的相同。我如何让它也检查属性?
发布于 2019-01-22 12:43:14
我在我的模式中缺少了一个required子句:
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
required: ['expected_id'] # <- this was missing
}如果我的端点生成的响应不包括expected_id键,则会引发测试错误。
https://stackoverflow.com/questions/54290563
复制相似问题