From 3b52d015ea07b1622ab8d137515aff8bd1d06ee3 Mon Sep 17 00:00:00 2001 From: pimterry Date: Sat, 7 Dec 2013 20:17:29 +0000 Subject: [PATCH 1/2] Track return values of spy functions --- src/core/base.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/core/base.js b/src/core/base.js index 9fe678cd..8505e1e1 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -56,11 +56,16 @@ getJasmineRequireObj().base = function(j$) { }), callTracker = new j$.CallTracker(), spy = function() { - callTracker.track({ + var callData = { object: this, args: Array.prototype.slice.apply(arguments) - }); - return spyStrategy.exec.apply(this, arguments); + }; + + callTracker.track(callData); + var returnValue = spyStrategy.exec.apply(this, arguments); + callData.returnValue = returnValue; + + return returnValue; }; for (var prop in originalFn) { From b1d4ab09af95419c37c1d79d5d894510c9a62ee5 Mon Sep 17 00:00:00 2001 From: pimterry Date: Sat, 7 Dec 2013 22:08:10 +0000 Subject: [PATCH 2/2] Add tests for call return value tracking --- spec/core/SpySpec.js | 10 ++++++++++ spec/core/integration/EnvSpec.js | 13 +++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spec/core/SpySpec.js b/spec/core/SpySpec.js index 5e2905b8..d69059b4 100644 --- a/spec/core/SpySpec.js +++ b/spec/core/SpySpec.js @@ -47,6 +47,16 @@ describe('Spies', function () { expect(trackSpy.calls.mostRecent().args[0].object).toEqual(contextObject); }); + + it("tracks the return value of calls", function () { + var spy = j$.createSpy(TestClass.prototype, TestClass.prototype.someFunction); + var trackSpy = spyOn(spy.calls, "track"); + + spy.and.returnValue("return value"); + spy(); + + expect(trackSpy.calls.mostRecent().args[0].returnValue).toEqual("return value"); + }); }); describe("createSpyObj", function() { diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index d6fd6c0a..755d2771 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -205,9 +205,14 @@ describe("Env integration", function() { var env = new j$.Env(); var originalFunctionWasCalled = false; - var subject = { spiedFunc: function() { originalFunctionWasCalled = true; } }; + var subject = { + spiedFunc: function() { + originalFunctionWasCalled = true; + return "original result"; + } + }; - var spy = env.spyOn(subject, 'spiedFunc'); + var spy = env.spyOn(subject, 'spiedFunc').and.returnValue("stubbed result"); expect(subject.spiedFunc).toEqual(spy); @@ -220,11 +225,15 @@ describe("Env integration", function() { expect(subject.spiedFunc.calls.count()).toEqual(1); expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['foo']); expect(subject.spiedFunc.calls.mostRecent().object).toEqual(subject); + expect(subject.spiedFunc.calls.mostRecent().returnValue).toEqual("stubbed result"); expect(originalFunctionWasCalled).toEqual(false); + subject.spiedFunc.and.callThrough(); subject.spiedFunc('bar'); expect(subject.spiedFunc.calls.count()).toEqual(2); expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['bar']); + expect(subject.spiedFunc.calls.mostRecent().returnValue).toEqual("original result"); + expect(originalFunctionWasCalled).toEqual(true); }); it("Mock clock can be installed and used in tests", function(done) {