6

我有以下 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

所以我的测试检查是否有一个对象数组,但检查嵌套对象中的属性是否符合预期。我如何让它也检查属性?

4

1 回答 1

3

required我的架构中缺少一个子句:

schema type: :array,
  items: {
    type: :object,
    properties: {
      expected_id: { type: "integer", format: "int32" },
    }
    required: ['expected_id'] # <- this was missing
  }

如果我的端点生成的响应不包含expected_id密钥,则会引发测试错误。

于 2019-01-22T12:43:14.627 回答