dimanche 5 août 2018

Mocha Using supertest , typeError: cannot read property 'call' of undefined

I am learning to use asynchronous testing for my basic Todo application. But I am finding a bug in developing a test suite for my application,

I wanted to delete a todo using my test suite.

Here is my code:

app.delete('/todos/:id', (req,res) => {
    const id = req.params.id ;

    if(!ObjectID.isValid(id))
        return res.status(400).send();

    Todo.findByIdAndRemove(id)
    .then((todo) => {
        res.send(todo);
    }, (error) => {
        res.status(404).send();
    });        
});

Here is the code of the test suite:

const todos = [{
        _id: new ObjectId(),
        text: 'first Todo'
    },
    {
        _id: new ObjectId(),
        text: 'Second Todo'
    }
];

beforeEach((done) => {
    Todo.remove({}).then(() => {
        return Todo.insertMany(todos);
        done();
    }).then(() => {
        done();
    }).catch(e => {
        console.log(e);
        done();
    });
});

describe('DELETE /todos/:id', () => {
    it('should delete a todo', (done) => {

        request(app)
            .delete(`/todos/${todos[1]._id.toHexString()}`)
            .expect(200)
            .end(done());
    });
});

I am finding a bug like:

 Uncaught TypeError: Cannot read property 'call' of undefined
      at Test.assert (node_modules/supertest/lib/test.js:181:6)
      at Server.assert (node_modules/supertest/lib/test.js:131:12)
      at emitCloseNT (net.js:1655:8)
      at _combinedTickCallback (internal/process/next_tick.js:135:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

Thanks

Aucun commentaire:

Enregistrer un commentaire