How to create a test for a function with a service inside?

Asked

Viewed 50 times

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?

1 answer

1


I don’t know the details of the implementation but it would be a way to force the error. For the examples of my attempts I will highlight only the excerpt I modified in relation to your code:

One way to try to force the 500 would be to send the id a negative number.

.send({
   id: -1,
   text: 'Teste de Observação',
})

A second way is to send the number 0.

.send({
   id: 0,
   text: 'Teste de Observação',
})

Another attempt is to send a special character

.send({
       id: '#@',
       text: 'Teste de Observação',
})

Another possibility is to send null or empty.

.send({
       id: null,
       text: 'Teste de Observação',
})
//----------------
.send({
       id: '',
       text: 'Teste de Observação',
})

Another possibility is not to send.

.send({
       text: 'Teste de Observação',
})

One thing we could try was to send a giant number (which force the overflow to the types long and int

.send({
       id: 99999999999999999999999999999999999999999999999999999999999999999999999,
       text: 'Teste de Observação',
})

If you still can’t, a possibility for the 500 is to make 2 consecutive registrations with the same id, this will force the database to error 500 if it is validating single id. Primary key validation (Constraint UNIQUE).

I hope somehow an error occurs 500.

  • So, it even gives the error, but Coverage points out that the Response.status() line is missing. Strange.

  • but the coverage increased? because sometimes bug the display because the command is being done on more than one line.

  • It did not increase. It goes on to say that it did not catch

  • 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. type error.response, if the error.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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.