Skip to content
Browse files

fixed bug I introduced on previous commit: don't autorestore if there…

… are more expectations
  • Loading branch information...
1 parent c1eb0af commit 807a2560e12029acea8c23fd09eb6d51a8b1abc6 @masylum masylum committed with May 20, 2011
Showing with 11 additions and 4 deletions.
  1. +6 −4 lib/gently/gently.js
  2. +5 −0 test/simple/test-gently.js
View
10 lib/gently/gently.js
@@ -131,10 +131,12 @@ Gently.prototype._stubFn = function(self, obj, method, name, args) {
if (expectation.count === 0) {
this.expectations.shift();
- // restore original if not a closure
- obj = expectation.obj;
- method = expectation.method;
- if (obj !== null && method !== null) {
+ // autorestore original if its not a closure
+ // and no more expectations on that object
+ has_more_expectations = this.expectations.reduce(function (memo, expectation) {
+ return memo || (expectation.obj === obj && expectation.method === method);
+ }, false);
+ if (obj !== null && method !== null && !has_more_expectations) {
if (typeof obj[method]._original !== 'undefined') {
obj[method] = obj[method]._original;
delete obj[method]._original;
View
5 test/simple/test-gently.js
@@ -160,7 +160,12 @@ test(function _stubFn() {
return 'stubbed foo';
});
+ gently.expect(OBJ2, 'foo', function() {
+ return "didn't restore yet";
+ });
+
assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), 'stubbed foo');
+ assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), "didn't restore yet");
assert.equal(OBJ2.foo(), 'bar');
assert.deepEqual(gently.expectations, []);
})();

0 comments on commit 807a256

Please sign in to comment.
Something went wrong with that request. Please try again.