我想用 openapi 3 描述我现有的 API,并用 dredd 证明我的描述。我知道 openapi 3 实现是实验性的,但我不使用任何尚不支持的元素。
这是我的 spec.yaml 的一部分
paths:
/login:
post:
summary: Login
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/login_request'
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/login_response'
"401":
description: Not authenticated
"404":
description: Other Error
...
schemas:
login_request:
type: object
properties:
n:
type: string
description: Username
example: super
p:
type: string
description: Password
example: user123
所以我可以测试正确输入凭据的情况(假设测试用户在那里),因为它们在规范中作为示例给出。但是错误案例呢?当然我可以在 hooks.js 文件中跳过它们:
var hooks = require('hooks');
hooks.before('/login > Login > 401', function (transaction, done) {
transaction.skip = true;
done();
});
hooks.before('/login > Login > 404', function (transaction, done) {
transaction.skip = true;
done();
});
注意:在我的情况下,可能是由于使用了 openapi 3,Dredd 的行为与这里所说的相矛盾:错误响应不会自动跳过。
但这是可取的吗?我也更愿意测试正确的错误反应。但是怎么做?我怀疑它现在是否受支持,但至少在理论上我可以在规范中插入多个示例,从而也包括导致错误的示例。但另一方面,任意错误的示例不会包含在文档中,不是吗?
这里的最佳做法是什么?