From f4ac38cc122d0b607b28ef84365ffbcb7ea08939 Mon Sep 17 00:00:00 2001 From: Andy Kent Date: Sat, 17 May 2008 00:19:32 +0100 Subject: [PATCH] mock expectations are now run recursively to avoid looping through each one every call. --- lib/smoke.mock.js | 9 +++------ spec/mock_spec.js | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/smoke.mock.js b/lib/smoke.mock.js index 9e33f25..687832b 100644 --- a/lib/smoke.mock.js +++ b/lib/smoke.mock.js @@ -33,15 +33,12 @@ Smoke.Mock = function(originalObj) { var expectation = new Smoke.Mock.Expectation(this, attr); if(this._expectations[attr]==undefined) this._expectations[attr] = []; this._expectations[attr].push(expectation); - var e = this._expectations[attr]; // closure var var previousFunction = this[attr]; var mock = this; this[attr] = function() { - for(var i in e) { - var result = e[i].run(arguments); - if(result!=undefined) return result; - }; - return previousFunction ? previousFunction.call(mock) : undefined; + var result = expectation.run(arguments); + if(result!=undefined) return result; + return previousFunction!=undefined ? previousFunction.apply(mock,arguments) : undefined; }; return expectation; }, diff --git a/spec/mock_spec.js b/spec/mock_spec.js index 0bcfda1..2477ea3 100644 --- a/spec/mock_spec.js +++ b/spec/mock_spec.js @@ -49,8 +49,8 @@ Screw.Unit(function() { mockObj = mock() mockObj.should_receive('foo').with_arguments('bar').and_return('foobar'); mockObj.should_receive('foo').with_arguments('mouse').and_return('cheese'); - expect(mockObj.foo('bar')).to(equal, 'foobar'); expect(mockObj.foo('mouse')).to(equal, 'cheese'); + expect(mockObj.foo('bar')).to(equal, 'foobar'); }); it("should allow mocking a method signature with arguments and setting expectations", function() { mockObj = mock()