【问题标题】:Schema testing using Mocha Timeout Error使用 Mocha Timeout Error 进行模式测试
【发布时间】:2018-05-12 14:02:54
【问题描述】:

这是我的架构:

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();
});

});

});

`

每次运行时我都会收到一条错误消息 " 错误:超过 2000 毫秒的超时。对于异步测试和挂钩,请确保调用了“done()”;如果返回 Promise,请确保它已解决。”

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

【问题讨论】:

    标签: node.js mongodb testing mocha.js schema


    【解决方案1】:

    我认为你有两个选择。

    1. 添加捕获
    2. 设置超时

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

    // 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);
      });
    });
    

    【讨论】:

    • 感谢您的回复,我按照您说的添加了 catch 和 timeout ,但错误仍然存​​在。它告诉我“确保“完成()”被调用;如果返回一个 Promise,确保它解决。如何确保调用 done()?
    • 我认为你的“done()”永远不会被你的逻辑调用。这意味着char.save() 我猜从未连接到 mongo。我认为您应该首先确保connect to mongo。 mongoose connect要不要参考这个页面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多