我正在编写一个简单的TypeScript快车应用程序,可以获取有关YouTube视频的信息。下面是路由器(挂载到/api
):
import express from 'express';
import ytdl from 'ytdl-core';
import bodyParser from 'body-parser';
const router = express.Router();
router.post('/info', bodyParser.json(), async (req, res, next) => {
const videoID = req.body.videoID
if (!ytdl.validateID(videoID)) {
const error = new Error('Invalid video ID')
res.status(400).json({error: error.toString()}).send;
next();
} else {
const videoFormats = await (await ytdl.getInfo(videoID)).formats;
res.setHeader('Content-type', 'application/json');
res.send(videoFormats);
}
});
export default router;
今天,我尝试用Mocha +柴编写我的第一次测试。api/info
端点需要POST,从主体中获取videoID
并使用JSON进行响应。到目前为止,这里是我对端点的测试。
import app from '../index'
import chaiHttp from 'chai-http'
import chai from 'chai'
chai.use(chaiHttp);
const expect = chai.expect;
const get = chai.request(app).get;
const post = chai.request(app).post;
describe('API', () => {
describe('POST /api/info', () => {
it('given videoID of a publically available video, responds with 200 OK and JSON that contains an array of objects', async () => {
const res = await post('/api/info')
.send({"videoID": "OrxmtDw4pVI"});
expect(res).to.have.status(200);
expect(res.body).to.be.an('array');
}).timeout(5000);
it('given an invalid videoID, responds with 400 and an Error', async () => {
const res = await post('/api/info')
.send({"videoID": "qwerty"});
expect(res).to.have.status(400);
});
});
});
测试结果如下:
API
POST /api/info
√ given videoID of a publically available video, responds with 200 OK and JSON that contains an array of objects (1028ms)
1) given an invalid videoID, responds with 400 and an Error
1 passing (1s)
1 failing
1) API
POST /api/info
given an invalid videoID, responds with 400 and an Error:
Error: Server is not listening
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ytb--down@1.0.0 test: `mocha -r ts-node/register src/**/*.spec.ts --exit`
npm ERR! Exit status 1
如您所见,第二个测试由于Server is not listening
错误而失败。如果我做了第一次测试,就不会发生。
我使用Postman手动测试此端点,发现没有导致服务器崩溃的问题,也没有引发错误。
我使用npm run test
命令启动测试,下面是来自package.json
的脚本
"scripts": {
"test": "mocha -r ts-node/register src/**/*.spec.ts --exit"
我怀疑测试套件本身有问题,但我无法找到解决方案。我在这里错过了什么?
发布于 2020-12-22 22:37:26
我想我找到了。
看来,对于像我这样的初学者来说,做的测试包含了一个错误,而不是一个明显的错误。
您不想想得到get
和post
快捷方式如下:
const get = chai.request(app).get;
const post = chai.request(app).post;
我认为真正的错误是我在这里调用了request()
。相反,应该抓住chai.request
的捷径
const request = chai.request;
现在测试正常。测试文件现在如下所示。
import server from '../index'
import chaiHttp from 'chai-http'
import chai from 'chai'
chai.use(chaiHttp);
const expect = chai.expect;
const request = chai.request;
describe('API', () => {
describe('POST /api/info', () => {
it('given videoID of a publically available video, responds with 200 and JSON that contains an array of objects', async () => {
const res = await request(server)
.post('/api/info')
.send({"videoID": "OrxmtDw4pVI"});
expect(res).to.have.status(200);
expect(res.body).to.be.an('array');
});
it('given an invalid videoID, responds with 400 and an Error', async () => {
const res = await request(server)
.post('/api/info')
.send({"videoID": "qwerty"});
expect(res).to.have.status(400);
expect(res.body.error).to.include('Error');
});
});
});
https://stackoverflow.com/questions/65415432
复制相似问题