I'm using Q to flatten out some callbacks in my unit tests and return a promise to mocha (which knows to wait until the promise is resolved before running the next test).
Originally I had this code:
it ('resetCount should cause the count to reset as if there were no documents yet.', function () {
// Now save user and check if its _id is what nextCount said.
user.save(function (err) {
should.not.exists(err);
user._id.should.eql(0);
// Call nextCount to check the next number. Should be one.
user.nextCount(function (err, count) {
should.not.exists(err);
count.should.eql(1);
// Now reset the count.
user.resetCount(function () {
// Call nextCount again to check that the next count is reset. Should be zero.
user.nextCount(function (err, count) {
should.not.exists(err);
count.should.eql(0);
done();
});
});
});
});
});
With Q I turned it into this:
it('resetCount should cause the count to reset as if there were no documents yet.', function () {
var userSchema = new mongoose.Schema({
name: String,
dept: String
});
userSchema.plugin(autoIncrement.plugin, 'User');
var User = db.model('User', userSchema);
// Create user and save it.
var user = new User({name: 'Charlie', dept: 'Support'});
// Now save user and check if its _id is what nextCount said.
q.nmcall(user, 'save')
.then(function (user) {
user._id.should.eql(0);
return q.nmcall(user, 'nextCount');
})
.then(function (count) {
count.should.eql(1);
return q.nmcall(user, 'resetCount');
})
.then(q.nmcall(user, 'nextCount'))
.then(function (count) {
count.should.eql(0);
}, function (err) {
should.not.exists(err);
});
});
I no longer have to call the done
callback because of how mocha understands to wait for the promise to resolve. I am new to Q so I'm wondering if I've done things the most efficient way. It seemed like calling node-style mongoose methods with q.nmcall
(alias for q.invoke
) made the most sense. However, if you think you could write the same code better then please let me know where I can improve it.