From 1d98a23b1476e31e06793769ad607199ea863fb5 Mon Sep 17 00:00:00 2001 From: pimterry Date: Sat, 7 Dec 2013 20:15:18 +0000 Subject: [PATCH 1/4] Add tests for call tracking in createSpy --- spec/core/SpySpec.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/core/SpySpec.js b/spec/core/SpySpec.js index fae73549..b84608dd 100644 --- a/spec/core/SpySpec.js +++ b/spec/core/SpySpec.js @@ -28,6 +28,20 @@ describe('Spies', function () { expect(spy.and).toEqual(jasmine.any(j$.SpyStrategy)); expect(spy.calls).toEqual(jasmine.any(j$.CallTracker)); }); + + it("tracks the argument of calls", function () { + var spy = j$.createSpy(TestClass.prototype, TestClass.prototype.someFunction); + spy("arg"); + expect(spy.calls.mostRecent().args).toEqual(["arg"]); + }); + + it("tracks the context of calls", function () { + var spy = j$.createSpy(TestClass.prototype, TestClass.prototype.someFunction); + var contextObject = { spyMethod: spy }; + contextObject.spyMethod(); + + expect(spy.calls[0].object).toEqual(contextObject); + }); }); describe("createSpyObj", function() { From b2e8de7bcd7851a8d38c40a427550d05ba3bd6aa Mon Sep 17 00:00:00 2001 From: pimterry Date: Sat, 7 Dec 2013 20:43:38 +0000 Subject: [PATCH 2/4] Mock callTracker in spy tests for better test isolation --- spec/core/SpySpec.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/spec/core/SpySpec.js b/spec/core/SpySpec.js index b84608dd..5e2905b8 100644 --- a/spec/core/SpySpec.js +++ b/spec/core/SpySpec.js @@ -31,16 +31,21 @@ describe('Spies', function () { it("tracks the argument of calls", function () { var spy = j$.createSpy(TestClass.prototype, TestClass.prototype.someFunction); + var trackSpy = spyOn(spy.calls, "track"); + spy("arg"); - expect(spy.calls.mostRecent().args).toEqual(["arg"]); + + expect(trackSpy.calls.mostRecent().args[0].args).toEqual(["arg"]); }); it("tracks the context of calls", function () { var spy = j$.createSpy(TestClass.prototype, TestClass.prototype.someFunction); + var trackSpy = spyOn(spy.calls, "track"); + var contextObject = { spyMethod: spy }; contextObject.spyMethod(); - expect(spy.calls[0].object).toEqual(contextObject); + expect(trackSpy.calls.mostRecent().args[0].object).toEqual(contextObject); }); }); From 14a8c2ca09b051189898fe48407d69d533249b84 Mon Sep 17 00:00:00 2001 From: pimterry Date: Sat, 7 Dec 2013 20:52:02 +0000 Subject: [PATCH 3/4] Move spy integration tests out of the unit test suite --- spec/core/EnvSpec.js | 17 ----------------- spec/core/integration/EnvSpec.js | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/spec/core/EnvSpec.js b/spec/core/EnvSpec.js index a175aa84..31389f09 100644 --- a/spec/core/EnvSpec.js +++ b/spec/core/EnvSpec.js @@ -61,26 +61,9 @@ describe("Env", function() { var originalFunctionWasCalled = false; var subject = { spiedFunc: function() { originalFunctionWasCalled = true; } }; - originalFunc = subject.spiedFunc; - var spy = env.spyOn(subject, 'spiedFunc'); expect(subject.spiedFunc).toEqual(spy); - - expect(subject.spiedFunc.calls.any()).toEqual(false); - expect(subject.spiedFunc.calls.count()).toEqual(0); - - subject.spiedFunc('foo'); - - expect(subject.spiedFunc.calls.any()).toEqual(true); - expect(subject.spiedFunc.calls.count()).toEqual(1); - expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['foo']); - expect(subject.spiedFunc.calls.mostRecent().object).toEqual(subject); - expect(originalFunctionWasCalled).toEqual(false); - - subject.spiedFunc('bar'); - expect(subject.spiedFunc.calls.count()).toEqual(2); - expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['bar']); }); }); diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index bc651780..977afc3b 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -201,6 +201,30 @@ describe("Env integration", function() { env.execute([secondSuite.id, firstSpec.id]); }); + it("Functions can be spied on and have their calls tracked", function () { + var originalFunctionWasCalled = false; + var subject = { spiedFunc: function() { originalFunctionWasCalled = true; } }; + + var spy = spyOn(subject, 'spiedFunc'); + + expect(subject.spiedFunc).toEqual(spy); + + expect(subject.spiedFunc.calls.any()).toEqual(false); + expect(subject.spiedFunc.calls.count()).toEqual(0); + + subject.spiedFunc('foo'); + + expect(subject.spiedFunc.calls.any()).toEqual(true); + expect(subject.spiedFunc.calls.count()).toEqual(1); + expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['foo']); + expect(subject.spiedFunc.calls.mostRecent().object).toEqual(subject); + expect(originalFunctionWasCalled).toEqual(false); + + subject.spiedFunc('bar'); + expect(subject.spiedFunc.calls.count()).toEqual(2); + expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['bar']); + }); + it("Mock clock can be installed and used in tests", function(done) { var globalSetTimeout = jasmine.createSpy('globalSetTimeout'), delayedFunctionForGlobalClock = jasmine.createSpy('delayedFunctionForGlobalClock'), From 83a692d5a887460153e36eca347d837f3d063a5b Mon Sep 17 00:00:00 2001 From: pimterry Date: Sat, 7 Dec 2013 22:16:24 +0000 Subject: [PATCH 4/4] Use the correct Jasmine version in the spy integration tests --- spec/core/integration/EnvSpec.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 977afc3b..d6fd6c0a 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -202,10 +202,12 @@ describe("Env integration", function() { }); it("Functions can be spied on and have their calls tracked", function () { + var env = new j$.Env(); + var originalFunctionWasCalled = false; var subject = { spiedFunc: function() { originalFunctionWasCalled = true; } }; - var spy = spyOn(subject, 'spiedFunc'); + var spy = env.spyOn(subject, 'spiedFunc'); expect(subject.spiedFunc).toEqual(spy);