lundi 1 février 2016

Mongoose unit test

I have the following mongoose unit test. The console log line in the article.save() callback prints out the assigned _id attributed to the entity after it has been saved from the database. However whenever I put in a breakpoint on that line or indeed on the first line in the corresponding unit test, the entity isn't available in the articles collection?

Can anyone explain why this should be? I'm struggling to make sense of this and would appreciate any help!

Thanks, Mark.

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

Aucun commentaire:

Enregistrer un commentaire