Merge pull request #473 from pimterry/callTrackingTests
Refactored the createSpy tests, to add unit tests for its part of call tracking
This commit is contained in:
@@ -61,26 +61,9 @@ describe("Env", function() {
|
|||||||
var originalFunctionWasCalled = false;
|
var originalFunctionWasCalled = false;
|
||||||
var subject = { spiedFunc: function() { originalFunctionWasCalled = true; } };
|
var subject = { spiedFunc: function() { originalFunctionWasCalled = true; } };
|
||||||
|
|
||||||
originalFunc = subject.spiedFunc;
|
|
||||||
|
|
||||||
var spy = env.spyOn(subject, 'spiedFunc');
|
var spy = env.spyOn(subject, 'spiedFunc');
|
||||||
|
|
||||||
expect(subject.spiedFunc).toEqual(spy);
|
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']);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,25 @@ describe('Spies', function () {
|
|||||||
expect(spy.and).toEqual(jasmine.any(j$.SpyStrategy));
|
expect(spy.and).toEqual(jasmine.any(j$.SpyStrategy));
|
||||||
expect(spy.calls).toEqual(jasmine.any(j$.CallTracker));
|
expect(spy.calls).toEqual(jasmine.any(j$.CallTracker));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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(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(trackSpy.calls.mostRecent().args[0].object).toEqual(contextObject);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("createSpyObj", function() {
|
describe("createSpyObj", function() {
|
||||||
|
|||||||
@@ -201,6 +201,32 @@ describe("Env integration", function() {
|
|||||||
env.execute([secondSuite.id, firstSpec.id]);
|
env.execute([secondSuite.id, firstSpec.id]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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 = 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']);
|
||||||
|
});
|
||||||
|
|
||||||
it("Mock clock can be installed and used in tests", function(done) {
|
it("Mock clock can be installed and used in tests", function(done) {
|
||||||
var globalSetTimeout = jasmine.createSpy('globalSetTimeout'),
|
var globalSetTimeout = jasmine.createSpy('globalSetTimeout'),
|
||||||
delayedFunctionForGlobalClock = jasmine.createSpy('delayedFunctionForGlobalClock'),
|
delayedFunctionForGlobalClock = jasmine.createSpy('delayedFunctionForGlobalClock'),
|
||||||
|
|||||||
Reference in New Issue
Block a user