Mocha超时2000ms错误

时间:2018-08-07 18:57:53

标签: javascript mongodb mocha

我是摩卡测试的新手,但是当我这样编写代码时

var assert=require('assert');
// ../ --->go up a directory
var MarioChar=require('../models/marioCharacter');

  //Describe tests
 //describe("description",function that executes tests)
 describe('Saving Records',function(done){
  //create tests
  //it blocks -describes a single tests
  //it("description",function that executes a test)
it('Saves a record to the database',function(){
    //if assert evaluates to true, the test passes
    //otherwise the test fails
    //marioCharacter model is expecting the properties of the object
    // that adhere to its set Schema
    var mario=new MarioChar({
      name:"mario"
    });
    mario.save().then(function(){
      console.log("saved");
      done();
    });

      //then need to save this character to the database
      //saves the character to the set database
      //save() is an async request so cannot assert save() directly
      //save implements the promise interface
});
//next test
});

但是,它起作用了,当我通过it块而不是describe块来完成操作时,它会读取错误“错误:超过2000毫秒的超时。对于异步测试和挂钩,请确保调用了“ done()”;如果返回承诺,确保其解决。”这里发生了什么?

1 个答案:

答案 0 :(得分:0)

测试承诺的更简单方法可能是使用async/await。您的测试看起来像

it('Saves a record to the database', async function(){
    var mario=new MarioChar({
      name:"mario"
    });
    await mario.save();
    console.log('saved');
    // Any assertions can go here after mario has been saved
});
相关问题