Mongoose mongodb保存子文档混乱?Mocha测试没有完成()

Mongoose mongodb saving sub documents confusion? Mocha test not getting to done()

本文关键字:测试 Mocha 保存 mongodb 文档 混乱 Mongoose      更新时间:2023-09-26

我正试图用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。。。