0
I have the code below and managed to create a test for it, when it enters error 400 and 200. However, I can’t get the test to enter the catch
.
I’m using Mocha and Chai.
myController.approve = (request, response) => {
let { id, text } = request.body;
if (!myController.idValidId(id)) {
response.status(400).send();
return;
}
myService.approve(id, text)
.then(() => {
response.send(200);
})
.catch(error => {
response.status(error.response.status).json({
approved: false,
error: "Erro ao aprovar documento",
});
});
};
My test:
describe('POST document', function () {
it('should return OK for the document', function (done) {
request(server)
.post('/my-url')
.send({
id: 1,
text: 'Teste de Observação',
})
.then(function(res){
expect(res.statusCode).to.equal(200);
done();
})
});
it('should return ERROR for document ID invalid', function (done) {
config.mockDocumentApproval();
request(server)
.post('/my-url')
.send({
id: 'not-a-number-valid',
text: 'Teste de Observação',
})
.then(function(res){
expect(res.statusCode).to.equal(400);
done();
})
});
it('should return error for document', function (done) {
// Erro ao entrar no tach
});
});
How do I get my test into error?
So, it even gives the error, but Coverage points out that the Response.status() line is missing. Strange.
– Diego Souza
but the coverage increased? because sometimes bug the display because the command is being done on more than one line.
– Danizavtz
It did not increase. It goes on to say that it did not catch
– Diego Souza
This your error.response.status is strange, try to return only a number 500 there where you arrow the status, I’m suspicious that this variable does not have this nesting. Type the
error.response
be Undefined or null know? This is causing an error within catch. One way to debuggar is to put a console.log(error), check if all values used are set. typeerror.response
, if theerror.response.status
exists. If so, check the type, as it may be returning a string, but you are using the object syntax. Then just do a parse.– Danizavtz