【发布时间】:2019-01-12 13:39:15
【问题描述】:
我正在学习对我的基本 Todo 应用程序使用异步测试。 但是我在为我的应用程序开发测试套件时发现了一个错误,
我想使用我的测试套件删除待办事项。
这是我的代码:
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();
});
});
这是测试套件的代码:
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());
});
});
我发现了一个类似的错误:
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)
谢谢
【问题讨论】:
-
当你运行这个时,你的节点服务器在运行吗?此外,由于您的 api 已被承诺,因此您无需致电
done()。您可以改为返回完整的提交承诺对象。
标签: node.js testing mocha.js supertest