【发布时间】:2016-04-10 13:27:04
【问题描述】:
我正在尝试在 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:
尝试将一些 console.log() 语句放入 post('save') 函数中,但似乎它们永远不会被调用。 LatLng.pre('save') 被调用,然后我想它只是坐在那里挂着......?
【问题讨论】:
标签: javascript node.js mongodb mongoose mocha.js