【问题标题】:Timeout while testing with mocha [duplicate]使用 mocha 测试时超时 [重复]
【发布时间】: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


【解决方案1】:

我不确定你为什么要这样做 Todo.remove(done);如果您不打算回调它,为什么要首先拥有它?

我会尝试改变: Todo.remove(done);

到:done();

希望对您有所帮助。

【讨论】:

  • 有人能解释一下为什么这被否决了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
  • 2017-02-26
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多