Skip to main content

根据测试数据验证 OpenAPI 规范

使用测试用例验证 OpenAPI

这主要由backstage-repo-tools schema openapi test在生成的规范中发现的任何错误可以是

1.手动修复,这通常与请求正文或响应正文更改有关。 2.使用 "backstage-repo-tools schema openapi test --update "自动修复。 3.修复测试用例。 这种情况可能发生在响应被模拟为

it('should return the right value', () => {
// We will assume that this is the actual response and update the spec accordingly.
// Ideally, this should be a fully populated return value.
const entity: Entity = {} as any;
app.get('/test', () => {
return entity;
});
const response = await request(app).get('/test');
expect(response.body).toEqual(entity);
});

会导致无效的规范验证。 返回值不具备类型中定义的所有属性。 尽可能避免这种情况。 更好的方法是

it('should return the right value', () => {
// We will assume that this is the actual response and update the spec accordingly.
// Ideally, this should be a fully populated return value.
const entity: Entity = {
apiVersion: 'a1',
kind: 'k1',
metadata: { name: 'n1' },
};
app.get('/test', () => {
return entity;
});
const response = await request(app).get('/test');
expect(response.body).toEqual(entity);
});

此外,对于更高级的使用情况,您可以运行yarn optic capture {PATH_TO_OPENAPI_FILE} --update interactive在引擎盖下,测试验证和更新由以下程序完成光学是一个支持 OpenAPI 规范和开发的伟大项目。 您可以找到更多选项这里.