Skip to content

Instantly share code, notes, and snippets.

@nagyv
Created April 29, 2012 07:10
Show Gist options
  • Save nagyv/2538496 to your computer and use it in GitHub Desktop.
Save nagyv/2538496 to your computer and use it in GitHub Desktop.
Testing mongoose pre-save with async
var mongoose = require('mongoose'),
async = require('async'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var MyS = new Schema({
feeling: String
});
MyS.pre('save', function(done){
async.parallel([
function(c1){c1();},
function(c1){c1();}
], function(err, res) {
console.log('pre1');
done(err);
});
});
MyS.pre('save', true, function(next, done){
next();
async.parallel([
function(c1){c1();},
function(c1){c1();}
], function(err, res) {
console.log('pre2');
done(err);
});
});
var My = mongoose.model('My', MyS);
describe("Testing async pre-save with async included", function(){
before(function(done){
var uri;
uri = process.env.MONGOOSE_TEST_URI ||
'mongodb://127.0.0.1:27017/test';
mongoose.connect(uri);
this.db = mongoose.connection;
this.db.once('open', function(){
done();
});
this.db.once('error', function (err) {
done(err);
});
});
after(function(){
this.db.close();
});
it("should save the object", function(done){
My.create({feeling: 'Love'}, function(err, stuff) {
console.log('created');
done(err);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment