【发布时间】:2015-10-02 21:09:22
【问题描述】:
我正在尝试使用 mocha 和 mongoose 编写测试用例。但是我编写的以下代码 sn-p 给了我错误“Todo”在每个“钩子之前: 错误:超过 2000 毫秒的超时。确保在此测试中调用了 done() 回调。”我无法解决这个问题。我是 node 的初学者。谁能帮我解决这个问题。提前致谢。
var Todo = require('../models/Todo'),
should = require('Should');
describe('Todo', function(){
beforeEach(function(done){
faketodo = {
name : 'xyz',
completed : true,
note : "This is test note"
}
Todo.remove(done);
});
describe('#save()', function(){
var todo;
beforeEach(function(done){
console.log('before each todo entry');
todo = new Todo(faketodo);
console.log('before each todo exit');
done();
});
it('should have name property', function(done){
todo.save(function(err, todo){
should.not.exist(err);
todo.should.have.property('name', 'xyz');
done();
});
});
it('should not save if name is not present', function(done){
todo.name = '';
todo.save(function(err, todo){
should.exist(err);
should.not.exist(todo.name);
done();
});
});
});
});
【问题讨论】:
-
错误提示您需要在测试中调用
done()回调。Todo.remove(done);可能是先完成通话还是先完成通话?.. -
@SebastianNette 是的,但是即使在 Todo.remove(done) 之后调用 done() 之后,我还是会再次超时。
-
我在您的代码中看到的唯一问题是您应该按照我关闭您的问题的答案中的说明执行
Todo.remove({}, done)。
标签: javascript mocha.js