首页 文章

使用Mocha超时错误进行架构测试

提问于
浏览
0

这是我的架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

//INDEX PAGE SCHEMA
const IndexSchema = new Schema({
  bio: String
});
const IndexPageData = mongoose.model('indexpagedata', IndexSchema);
module.exports = IndexPageData;

这是我的摩卡代码:

const assert = require('assert');
const mocha = require('mocha');
const MarioChar = require('../models/model');

// Describe our tests
describe('Saving records', function(){

  // Create tests
  it('Saves a record to the database', function(done){

var index = new IndexPageData({
  bio: 'Mario ENTER BIO HERE'
});

char.save().then(function(){
  assert(char.isNew === false);
  done();
});

});

});

`

每次运行此操作时,我都会收到错误消息“错误:超出2000ms超时 . 对于异步测试和挂钩,请确保调用”done()“;如果返回Promise,请确保它已解析 . ”

mongoDB已经在后台运行,所以我不知道是什么原因导致了这个问题 .

1 回答

  • 0

    我认为你有两种选择 .

    • 添加捕获

    • 设置超时

    我通常将它们都插入到我的代码中 .

    // Add catch 
    describe('Saving records', function(){
      // Create tests
      it('Saves a record to the database', function(done){
    
        var index = new IndexPageData({
            bio: 'Mario ENTER BIO HERE'
        });
    
        char.save().then(function(){
            assert(char.isNew === false);
            done();
        }).catch(done);
      });
    });
    
    // Add time out
    describe('Saving records', function(){
    
      // Create tests
      it('Saves a record to the database', function(done){
        this.timeout(3000); // milli seconds
        var index = new IndexPageData({
            bio: 'Mario ENTER BIO HERE'
        });
    
        char.save().then(function(){
            assert(char.isNew === false);
            done();
        }).catch(done);
      });
    });
    

相关问题