diff --git a/AUTHORS b/AUTHORS index cb4105d2d..41845806f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -40,3 +40,4 @@ Tomás Corral Casas, amischol@gmail.com Tristan Koch, tristan.koch@1und1.de Will Butler, will@butlerhq.com Wesley Walser, waw325@gmail.com +Jonny Reeves, github@jonnyreeves.co.uk diff --git a/lib/sinon.js b/lib/sinon.js index 4116929d7..f518beb23 100644 --- a/lib/sinon.js +++ b/lib/sinon.js @@ -49,6 +49,10 @@ var sinon = (function (buster) { } } + function isRestorable (obj) { + return typeof obj === "function" && typeof obj.restore === "function" && obj.restore.sinon; + } + var sinon = { wrapMethod: function wrapMethod(object, property, method) { if (!object) { @@ -299,6 +303,19 @@ var sinon = (function (buster) { throw new TypeError("The constructor should be a function."); } return sinon.stub(sinon.create(constructor.prototype)); + }, + + restore: function (object) { + if (object !== null && typeof object === "object") { + for (var prop in object) { + if (isRestorable(object[prop])) { + object[prop].restore(); + } + } + } + else if (isRestorable(object)) { + object.restore(); + } } }; diff --git a/test/sinon_test.js b/test/sinon_test.js index 2e98c759c..ace589ab8 100644 --- a/test/sinon_test.js +++ b/test/sinon_test.js @@ -500,5 +500,40 @@ buster.testCase("sinon", { }); } } + }, + + ".restore": { + "restores all methods of supplied object": function () { + var methodA = function () {}; + var methodB = function () {}; + var obj = { methodA: methodA, methodB: methodB }; + + sinon.stub(obj); + sinon.restore(obj); + + assert.same(obj.methodA, methodA); + assert.same(obj.methodB, methodB); + }, + + "only restores restorable methods": function () { + var stubbedMethod = function () {}; + var vanillaMethod = function () {}; + var obj = { stubbedMethod: stubbedMethod, vanillaMethod: vanillaMethod }; + + sinon.stub(obj, "stubbedMethod"); + sinon.restore(obj); + + assert.same(obj.stubbedMethod, stubbedMethod); + }, + + "restores a single stubbed method": function () { + var method = function () {}; + var obj = { method: method }; + + sinon.stub(obj); + sinon.restore(obj.method); + + assert.same(obj.method, method); + } } });