0

注意:我完全是 nodejs 的菜鸟。请以具有其他编程语言经验但创建他的第一个 nodejs 应用程序的人可以理解的方式解释您的答案 =)。

我正在尝试编写一个简单的测试应用程序,它应该根据 OpenApi 3.0 规范自动测试外部服务。

我整理了一些示例代码,以尝试针对使用 mocha 和 chai 实现 API 的外部服务自动测试 OpenApi 规范。

现在我的问题似乎是找不到我的 mocha 测试模块。

我收到以下错误消息:

> client_test@1.0.0 test /Users/user1/dev/app/app-rt/client_test
> mocha ./test/**/*.test.js

/Users/user1/dev/app/app-rt/client_test/node_modules/yargs/yargs.js:1163
else throw err
^

Error: The "path" argument must be an absolute filepath

我的 package.json:

{
    "name": "client_test",
    "version": "1.0.0",
    "description": "Client Tester",
    "main": "index.js",
    "scripts": {
        "test": "mocha ./test/**/*.test.js"
    },
    "license": "UNLICENSED",
    "dependencies": {
        "chai-http": "^4.3.0",
        "chai-openapi-response-validator": "^0.2.4",
        "mocha": "^6.2.0",
        "nock": "^11.3.2"
    }
}

test/client_all.test.js 中的小测试应用程序:

// Set up Chai
const chai = require('chai');
const expect = chai.expect;

// Import this plugin
const chaiResponseValidator = require('chai-openapi-response-validator');

const baseUrl = 'http://localhost:8081';

// Load an OpenAPI file (YAML or JSON) into this plugin
chai.use(chaiResponseValidator('./spec/app.json'));

// Get an HTTP response using chai-http
chai.use(require('chai-http'));

// Write your test (e.g. using Mocha)
describe('GET /zones', function() {
         it('should satisfy OpenAPI spec', async function() {

            const res = chai.request(baseUrl).get('/zones');

            expect(res.status).to.equal(200);

            // Assert that the HTTP response satisfies the OpenAPI spec
            expect(res).to.satisfyApiSpec;
           });
});

您能帮我弄清楚为什么路径无法解析,以及如何解决吗?如果您认为我做错了,也可以随意评论测试代码。

4

1 回答 1

0

问题出在包chai-openapi-response-validator上。尝试这样的事情:

// Import this plugin
const chaiResponseValidator = require('chai-openapi-response-validator');

const baseUrl = 'http://localhost:8081';

// New code
const path = require('path')
const specPath = path.resolve('./spec/app.json')

// Load an OpenAPI file (YAML or JSON) into this plugin
chai.use(chaiResponseValidator(specPath));

确保文件的路径app.json是相对于package.json文件的,或者使用其他方法将其转换为绝对路径。

于 2019-09-04T14:13:39.860 回答