mongooseunit testing

我有以下mongooseunit testing。 article.save()callback中的控制台日志行将从数据库中保存后,打印出归属于该实体的已分配_id。 但是,无论何时我在该行上或者在相应的unit testing的第一行中都放置了断点,该实体在文章集合中都不可用?

任何人都可以解释为什么这应该是? 我努力了解这一点,并会感谢任何帮助!

谢谢,马克。

var app = require('../../server.js'), should = require('should'), mongoose = require('mongoose'), User = mongoose.model('User'), Article = mongoose.model('Article'); var user, article; describe('Article Model Unit Tests:', function () { beforeEach(function (done) { user = new User({ firstName: 'Full', lastName: 'Last', displayName: 'Full Name', email: 'test@test.com', username: 'username', password: 'password' }); user.save(function () { article = new Article({ title: 'Article Title', content: 'Content Article', user: user }); article.save(function (err, savedArticle) { console.log(savedArticle._id); }); }); done(); }); describe('Testing the save method', function () { it('Should be able to save without problems', function () { article.save(function (err) { should.not.exist(err); }); }); it('Should not be able to save without title', function () { article.title = ''; article.save(function (err) { should.exist(err); }); }); }); afterEach(function(done) { Article.remove(function(){ User.remove(function(){ done(); }); }); }); }); 

我认为完成的调用应该被移到article.save里面

现在,done()几乎总是在两次保存完成之前被调用,如果完成了被移动,那么beforeeach块将只在两次保存完成时完成