0
節點7.4.0 /快速4.14.0 /打字稿2.1.5/M 3..1.2 /柴3.5.0 /柴HTTP 3.0.0節點/快遞API測試未發現故障
我想測試採用摩卡/柴沒有找到資源,但在運行應用程序時,它的失敗顯示404錯誤
HeroRouter.ts
....
/**
* GET one hero by id
*/
public getOne(req: Request, res: Response, next: NextFunction) {
let query = parseInt(req.params.id);
let hero = Heroes.find(hero => hero.id === query);
if (hero) {
res.status(200)
.send({
message: 'Success',
status: res.status,
hero
});
}
else {
res.status(404)
.send({
message: 'No hero found with the given id.',
status: res.status
});
}
}
運行後會顯示以下日誌:
$ http localhost:3000/api/v1/heroes/-32
HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 46
Content-Type: application/json; charset=utf-8
Date: Mon, 06 Feb 2017 12:53:34 GMT
ETag: W/"2e-W+nFWN7hcnJDQ4ZwWcRkpQ"
X-Powered-By: Express
{
"message": "No hero found with the given id."
}
BUT試圖寫測試將失敗:
herotest.ts 描述( 'GET API/V1 /英雄/:ID',()=> {
it('should respond with Not Found',() => {
return chai.request(app).get('/api/v1/heroes/-32')
.then(res => {
expect(res.status).to.equal(404);
});
});
});
NPM測試失敗
GET /api/v1/heroes/-32 404 1.209 ms - 46
1) should respond with Not Found
1 failing
1) GET api/v1/heroes/:id should respond with Not Found:
Error: Not Found
at Test.Request.callback (node_modules/superagent/lib/node/index.js:626:17)
at node_modules/superagent/lib/node/index.js:795:18
at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/parsers/json.js:16:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
解決。按照謝爾蓋的回答 -
it('should respond with Not Found',() => {
return chai.request(app).get('/api/v1/heroes/-32')
.catch(function (res) {
expect(res.status).to.equal(404);
});
});
感謝澄清它... – erwin
@erwin我只是增加了一個鏈接到相關gitbub問題 –