Knoten 7.4.0/express 4.14.0/Typoskript 2.1.5/m 3..1.2/chai 3.5.0/chai-http 3.0.0Node/Express API Test nicht bestehen nicht gefunden
Ich versuche, eine Ressource nicht gefunden mit Mocha/Chai zu testen, aber es versagt, wenn die App läuft zeigt die 404-Fehler
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
});
}
}
Lauf das folgende Protokoll wird angezeigt:
$ 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."
}
sondern versuchen, Test zu schreiben fehl:
herotest.ts beschreiben ('api/v1/Helden GET /: 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 Test verlassen hatte
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)
GELÖST. wie pro Sergey Antwort -
it('should respond with Not Found',() => {
return chai.request(app).get('/api/v1/heroes/-32')
.catch(function (res) {
expect(res.status).to.equal(404);
});
});
Dank für die Klärung ... – erwin
@erwin Ich habe gerade einen Link zu dem entsprechenden hinzugefügt Gitbub Problem –