简体   繁体   English

猫鼬mongodb保存子文件混乱吗? 摩卡测试未完成()

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

I'm trying to write some tests in mocha to save a document and some subdocuments inside of the beforeEach method, but it appears to never reach done and then proceeds to time out. 我正在尝试在mocha中编写一些测试,以将文档和某些子文档保存在beforeEach方法内,但是它似乎永远无法done ,然后超时。

Here is the schema: 这是模式:

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

Here is the beforeEach : 这是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...
              });
            });
          });

        });
      });
    });

Here is the test I'm trying to write: 这是我要编写的测试:

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

Sorry about the formatting, it got screwed up while copy and pasting it. 对不起格式,它在复制和粘贴时搞砸了。

I'm confused as to what the problem is...all I get when I try to run the test is it says that the beforeEach has timed out and to call done . 我对问题是什么感到困惑...当我尝试运行测试时,我得到的只是它说beforeEach已经超时并且调用了done But done is being called, therefor I assume something is going on while it's actually saving, but there is no errorsbeing produced. 但是done ,因此我认为实际上在保存时正在发生某些事情,但是没有产生错误。 I've tried setting mochas timeout as high as 30 seconds to see if it was mongodb lagging, to no avail. 我尝试将Mochas timeout设置为30秒,以查看是否是mongodb滞后,但无济于事。 Totally confused at the moment, hoping one of you amazing people on SO can rescue me :) 此刻完全感到困惑,希望你们当中一位很棒的人能救我:)

Thanks for taking the time to read my post and help out! 感谢您抽出宝贵的时间阅读我的文章并提供帮助!

Update : 更新

So I put a few console.log() statements inside of Walk.pre('save') , as well as in LatLng.pre('save') and it appears that Walk.pre('save') never getting called, but the LatLng.pre('save') is... 所以我把一些console.log()内的语句Walk.pre('save')以及在LatLng.pre('save')看来, Walk.pre('save')从来没有得到所谓的,但是LatLng.pre('save')是...

Update2: 更新2:

Tried putting some console.log() statments inside of the post('save') functions and it appears they never get called. 试图将一些console.log()语句放入post('save')函数中,看来它们从未被调用过。 LatLng.pre('save') is getting called and then I'm thinking it's just sitting there hanging...? LatLng.pre('save')被调用,然后我认为它只是挂在那儿...?

Wow, can't believe I missed that. 哇,不敢相信我错过了。 Simply forgot to call next in my LatLng.pre('save') ... 只是忘了在我的LatLng.pre('save')调用next ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM