Mongoose mongodb保存子文档混乱?Mocha测试没有完成()
Mongoose mongodb saving sub documents confusion? Mocha test not getting to done()
我正试图用mocha编写一些测试,以在beforeEach
方法中保存文档和一些子文档,但它似乎永远无法到达done
,然后超时。
这是模式:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var LatLng = new Schema({
id: Schema.ObjectId,
created_at: Date,
accuracy: {
type: Number,
required: true
},
latitude: {
type: Number,
required: true
},
longitude: {
type: Number,
required: true
}
});
LatLng.pre('save', function(next){
if(!this.created_at)
this.created_at = new Date();
});
var Walk = new Schema({
id: Schema.ObjectId,
created_at: Date,
updated_at: Date,
description: String,
elapsedTime: Number,
distance: Number,
waypoints: [LatLng],
_user: {
type: Schema.ObjectId,
ref: 'User',
required: true
}
});
Walk.pre('save', function(next){
var now = new Date();
this.updated_at = now;
if(!this.created_at)
this.created_at = now;
next();
});
Walk.pre('update', function(next){
this.updated_at = new Date();
});
Walk.pre('findOneAndUpdate', function(next){
this.updated_at = new Date();
});
Walk.pre('findByIdAndUpdate', function(next){
this.updated_at = new Date();
});
module.exports = mongoose.model('Walk', Walk);
这是beforeEach
:
beforeEach(function(done){
var _this = this;
//create temporary user
var userData = { username: 'bob', password: 'password123' };
var user = new User(userData)
.save(function(err, user){
if(err) done(err);
_this.user = user;
//login with temporary user
req.post('/login')
.send(userData)
.end(function(err, res){
if(err) done(err);
//create temporary models
var walk1 = new Walk({
description: 'description1',
elapsedTime: 10000,
distance: 5000,
_user: res.body.user._id,
waypoints: [
{"accuracy":11, "latitude":44.06523257, "longitude":-123.06101363},
{"accuracy":12, "latitude":44.06525829, "longitude":-123.06100709},
{"accuracy":11, "latitude":44.06523424, "longitude":-123.06099261},
{"accuracy":11, "latitude":44.0652201, "longitude":-123.06097669}
]
});
var walk2 = new Walk({
description: 'description1',
elapsedTime: 10000,
distance: 5000,
_user: res.body.user._id,
waypoints: [
{"accuracy":11, "latitude":44.06523424, "longitude":-123.06099261},
{"accuracy":11, "latitude":44.0652201, "longitude":-123.06097669},
{"accuracy":11, "latitude":44.06521917,"longitude":-123.06098176}
]
});
var walk3 = new Walk({
description: 'description1',
elapsedTime: 10000,
distance: 5000,
_user: res.body.user._id,
waypoints: [
{"accuracy":6, "latitude":44.06528592, "longitude":-123.06087405},
{"accuracy":4, "latitude":44.06528038, "longitude":-123.06088851},
{"accuracy":4, "latitude":44.06528185, "longitude":-123.06088036}
]
});
//save temporary models
walk1.save(function(err, walk){
console.log('got here'); // <--Never gets here
if(err) return done(err);
_this.walk1 = walk1;
console.log(walk);
walk2.save(function(err, walk){
if(err) return done(err);
_this.walk2 = walk2;
console.log(walk);
walk3.save(function(err, walk){
if(err) return done(err);
_this.walk3 = walk3;
console.log(walk);
done(); // <---Done is called here, so it's not this...
});
});
});
});
});
});
这是我要写的测试:
it.only('should create a new walk', function(done){
var _this = this;
var walk = {
description: "this is a description",
elapsedTime: 100000,
distance: 5000,
waypoints:
[{ "accuracy":11, "latitude":44.06523257, "longitude":-123.06101363 },
{ "accuracy": 12, "latitude": 44.06525829,"longitude": -123.06100709},
{ "accuracy": 21, "latitude":44.06521917, "longitude":-123.06098176 }]
};
var query = { description: walk.description};
req.post('/login')
.send(userData)
.end(function(err, res){
if(err) return done(err);
req.post('/walks')
.send(walk)
.expect(200)
.end(function(err, res){
if(err) return done(err);
res.body.success.should.equal(true);
Walk.findOne(_this.query, function(err, walk){
if(err) return done(err);
walk.should.not.be.null();
walk.description.should.match(_this.walk.description);
walk.elapsedTime.should.equal(_this.walk.elapsedTime);
walk.distance.should.equal(_this.walk.distance);
walk.waypoints.should.be.type('object');
done();
});
});
});
});
很抱歉格式化,它在复制和粘贴时出错了。
我很困惑问题出在哪里……当我尝试运行测试时,我得到的只是它说beforeEach
超时并调用done
。但是done
正在被调用,因此我认为在它实际保存时发生了一些事情,但没有产生错误。我试着将mochas timeout
设置为30秒,看看它是否是mongodb滞后的,但没有成功。此刻完全困惑,希望你们中的一个了不起的人能救我:)
感谢您花时间阅读我的帖子并提供帮助!
更新:
所以我在Walk.pre('save')
和LatLng.pre('save')
中放入了一些console.log()
语句,似乎Walk.pre('save')
从未被调用,但LatLng.pre('save')
是…
更新2:
尝试在post('save')
函数中放入一些console.log()
语句,但它们似乎从未被调用。LatLng.pre('save')
接到电话,然后我想它只是坐在那里挂着。。。?
哇,真不敢相信我错过了。只是忘了在我的LatLng.pre('save')
中呼叫next
。。。
相关文章:
- 如何在Mocha/Chai中测试JS原型(非模块)
- 当我尝试运行mocha测试时,没有定义Worker
- Mocha js测试字符串不相等时不显示diff
- 我们能在&在块到mocha的特定测试用例(it)之后
- 如何使用console.log语句在Mocha中测试函数
- Mocha/Chai测试链接到函数返回断言错误
- 如何按照承诺使用mocha/chai/chai测试ES7异步函数
- Mocking console.log()/MOCHA测试框架中的任何其他函数
- 用mocha测试异步函数
- Mocha测试框架的test.describe返回值
- 使用节点调试器调试ES6 Mocha测试
- 如何用mocha和rewire模拟node.js单元测试中的Q方法
- Mocha测试套件因未捕获的异常而暂停
- 如何在JavaScript中使用Mocha和assert.throws测试参数的值
- Node.js+Mocha+Should.js如何测试事件发射器抛出的错误
- Javascript Mocha测试-测试函数表达式中的函数表达式
- Mocha网络套接字测试https服务器
- 编写Karma+Mocha测试,同时使用依赖注入和“完成”
- 单元测试节点模块与mocha,模块变量行为怪异
- 用Blanket测试Mocha的覆盖率;不起作用