Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions lib/sinon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
};

Expand Down
35 changes: 35 additions & 0 deletions test/sinon_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});