From 5655e8f4d2d2e0ee4a221e1c35b24d6300781b55 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Sat, 30 Mar 2013 17:20:32 -0700 Subject: [PATCH] tests only: no afterCreate/afterUpdate on errors Added tests to ensure that the afterCreate/afterUpdate handlers are not run if the adapter returns an error. --- test/hooks.test.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/hooks.test.js b/test/hooks.test.js index 837b2e95..78841853 100644 --- a/test/hooks.test.js +++ b/test/hooks.test.js @@ -72,6 +72,20 @@ describe('hooks', function() { (new User).save(); }); + it('afterCreate should not be triggered on failed create', function(done) { + var old = User.schema.adapter.create; + User.schema.adapter.create = function(modelName, id, cb) { + cb(new Error('error')); + } + + User.afterCreate = function() { + throw new Error('shouldn\'t be called') + }; + User.create(function (err, user) { + User.schema.adapter.create = old; + done(); + }); + }); }); describe('save', function() { @@ -202,6 +216,23 @@ describe('hooks', function() { user.updateAttributes({name: 1, email: 2}); }); }); + + it('afterUpdate should not be triggered on failed save', function(done) { + User.afterUpdate = function() { + throw new Error('shouldn\'t be called') + }; + User.create(function (err, user) { + var old = User.schema.adapter.save; + User.schema.adapter.save = function(modelName, id, cb) { + cb(new Error('error')); + } + + user.save(function(err) { + User.schema.adapter.save = old; + done(); + }); + }); + }); }); describe('destroy', function() {