Javascript 摩卡咖啡超时2000ms错误
我不熟悉mocha测试,但是当我编写代码时Javascript 摩卡咖啡超时2000ms错误,javascript,mongodb,mocha.js,Javascript,Mongodb,Mocha.js,我不熟悉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(don
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块而不是descripe块传递done时,它会读取错误“error:超时超过2000ms。对于异步测试和钩子,请确保调用了“done()”;如果返回承诺,请确保它已解析。“这里发生了什么?” 测试承诺的更简单方法可能是使用。你的测试看起来像
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
});
我认为done参数应该在单独的测试中。就像
it('将记录保存到数据库',函数(done){…}
Ya但是当我这样做时,它不起作用了。你能把它从描述函数中删除吗?对不起,我看错了。摩卡应该支持承诺,所以你不需要done
,除非你在使用回调。你可以在承诺完成后做断言-即在then()中
,或者您可以看到我关于如何使用async/await的答案