我正在尝试使用Mocha + Chai自动执行API的测试。
现在,我有以下代码:
it("Correct request", function(done) {
chai.request(url)
.get(endpoint)
.set("Authorization", "bearer " + token)
.end(function(err, res) {
expect(res).to.have.status(200);
userAttrs.forEach((attr) => {
expect(res.body[attr]).to.not.be.undefined;
})
done();
})
});
it("No authorization request", function(done) {
chai.request(url)
.get(endpoint)
.end(function(err, res) {
expect(res).to.have.status(401);
expect(res).to.be.undefined;
done();
})
});
在控制台中,我得到以下结果:
c:\workspace\functional-tests>npm test
> @ test c:\workspace\functional-tests
> mocha --timeout 50000 ./test/app/users/data.js
APP endpoints
GET /users/data
√ Correct request (427ms)
1) No authorization request
1 passing (1s)
1 failing
1) APP endpoints
GET /users/data
No authorization request:
Uncaught ReferenceError: expected is not defined
at c:\workspace\functional-tests\test\app\users\data.js:54:21
at Test.Request.callback (node_modules\superagent\lib\node\index.js:716:12)
at IncomingMessage.parser (node_modules\superagent\lib\node\index.js:916:18)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
npm ERR! Test failed. See above for more details.
我的问题是...如何设置异常格式?我尝试使用功能catch,但是它使两个测试都通过(在控制台中,它说2个测试通过了,但打印出错误)。
我也尝试过
期望(res).to.throw(错误);
和
完成(错误);
我希望它显示一条错误消息以及我要定义的测试。
预先感谢您!