猫鼬单元测试

时间:2016-02-01 23:35:33

标签: node.js mongodb unit-testing

我有以下猫鼬单元测试。在从数据库保存实体后,article.save()回调中的控制台日志行打印出归属于该实体的已分配_id。但是,每当我在相应的单元测试中的该行或实际上的第一行放置断点时,该实体在文章集合中不可用?

任何人都可以解释为什么会这样吗?我正在努力理解这一点,并希望得到任何帮助!

谢谢, 标记

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

1 个答案:

答案 0 :(得分:1)

我认为完成的调用应该在article.save中移动

就像现在一样,在两次保存完成之前几乎总是会调用done(),如果完成了移动,那么只有两次保存完成后才能完成之前的块