摩卡 test.Timeout 2000 毫秒

Mocha test.Timeout 2000ms

我做错了什么?我正在尝试将 phones 保存到我的数据库,但 mocha 显示超时超过 2000 毫秒。 我的 phone 架构

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

const phoneSchema = new Schema({
    company: String,
    modelname: String,
    numbername: String,
    picture: String,
    price: String,
});

const Phone = mongoose.model('phone', phoneSchema);

module.exports = Phone;

和我的测试文件:

    const mocha = require('mocha');
const assert = require('assert');
const Phone = require('../models/phone-model');


//describe tests
describe('Saving records', function(){
    //create tests
    it('Saves phone to the db',function(done){

        var phone1 = new Phone({
            company: "Samsung",
            modelname: "Galaxy",
            numbername: "S5",
            picture: "https://drop.ndtv.com/TECH/product_database/images/2252014124325AM_635_samsung_galaxy_s5.jpeg",
            price: "12500P"
        });

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

    });

});

我不明白我在这里做错了什么..在这之前它总是对我有用。 错误:超时超过 2000 毫秒。对于异步测试和挂钩,确保 "done()" 被调用;如果返回 Promise,请确保它已解析。 (C:\Users\User\Desktop\Oauth\test\phone_test.js)

正如评论所说,如果 save 方法的承诺得到解决,您只会调用 done,这意味着如果您的数据库发送错误响应,您的测试将超时。

由于您正在处理承诺,请尝试使用 mocha 的 built-in promise support 而不是 done 回调:

const mocha = require('mocha');
const assert = require('assert');
const Phone = require('../models/phone-model');

describe('Saving records', function(){
    // Note there is no `done` argument here
    it('Saves phone to the db',function(){

        var phone1 = new Phone({
            company: "Samsung",
            modelname: "Galaxy",
            numbername: "S5",
            picture: "https://drop.ndtv.com/TECH/product_database/images/2252014124325AM_635_samsung_galaxy_s5.jpeg",
            price: "12500P"
        });

        // Note returning the promise here...
        return phone1.save().then(function(){
            assert(phone1.isNew === false);
        });

    });

});

让我们知道您在此之后看到了什么。希望您会看到一条实际的错误消息,这将帮助您找出问题所在。您的数据库也很可能需要超过 2000 毫秒的响应时间,尽管这种可能性更大。