【发布时间】:2018-11-13 22:43:18
【问题描述】:
我做错了什么?我正在尝试将手机保存到我的数据库,但 mocha 说超时超过 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();
});
});
});
我不明白我在这里做错了什么......在此之前它总是对我有用。 错误:超过 2000 毫秒的超时。对于异步测试和钩子,确保调用了“done()”;如果返回 Promise,请确保它已解决。 (C:\Users\User\Desktop\Oauth\test\phone_test.js)
【问题讨论】:
-
一个简单的解释是
save()没有解析。尝试添加catch()。 -
这是从规范中返回承诺是一个好主意而使用
done不是的少数原因之一。 -
不要添加捕获。只需返回您的
then的承诺即可。如果它拒绝,测试将失败。不需要冗余的故障处理程序。
标签: javascript node.js mongodb testing mocha.js