首页 文章

摩卡测试 . 超时2000ms

提问于
浏览
0

我做错了什么?我正在尝试将手机保存到我的数据库,但是摩卡说超过了2000毫秒的超时 . 我的手机架构

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

    });

});

我不明白我在这里做错了什么..在此之前总是对我有用 . 错误:超出2000ms的超时 . 对于异步测试和挂钩,确保调用“done()”;如果返回Promise,请确保它已解决 . (C:\用户\用户\桌面\的Oauth \测试\ phone_test.js)

1 回答

  • 1

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

    既然你're dealing with promises, try using mocha' s 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毫秒来响应,尽管这更有可能 .

相关问题