Squashed spy refactor and new spy syntax

Jasmine spies now have a 'and' property which allows the user to
change the spy's execution strategy-- such as '.and.callReturn(4)'
and a 'calls' property which allows inspection of the calls a spy
has received.

* This is a breaking change *

There is a CallTracker that keeps track of all calls and arguments
and a SpyStrategy which determines what the spy should do when it
is called.
This commit is contained in:
Davis W. Frank & Sheel Choksi
2013-07-17 23:11:55 -07:00
committed by Colin O'Byrne and JR Boyens
parent 18c30566bd
commit 3847557bbc
32 changed files with 692 additions and 413 deletions

View File

@@ -13,20 +13,20 @@ describe('Spies', function () {
};
env.spyOn(TestClass, 'someFunction');
expect(TestClass.someFunction.wasCalled).toEqual(false);
expect(TestClass.someFunction.callCount).toEqual(0);
expect(TestClass.someFunction.calls.any()).toEqual(false);
expect(TestClass.someFunction.calls.count()).toEqual(0);
TestClass.someFunction('foo');
expect(TestClass.someFunction.wasCalled).toEqual(true);
expect(TestClass.someFunction.callCount).toEqual(1);
expect(TestClass.someFunction.mostRecentCall.args).toEqual(['foo']);
expect(TestClass.someFunction.mostRecentCall.object).toEqual(TestClass);
expect(TestClass.someFunction.calls.any()).toEqual(true);
expect(TestClass.someFunction.calls.count()).toEqual(1);
expect(TestClass.someFunction.calls.mostRecent().args).toEqual(['foo']);
expect(TestClass.someFunction.calls.mostRecent().object).toEqual(TestClass);
expect(originalFunctionWasCalled).toEqual(false);
TestClass.someFunction('bar');
expect(TestClass.someFunction.callCount).toEqual(2);
expect(TestClass.someFunction.mostRecentCall.args).toEqual(['bar']);
expect(TestClass.someFunction.calls.count()).toEqual(2);
expect(TestClass.someFunction.calls.mostRecent().args).toEqual(['bar']);
});
it('should allow you to view args for a particular call', function() {
@@ -40,9 +40,8 @@ describe('Spies', function () {
TestClass.someFunction('foo');
TestClass.someFunction('bar');
expect(TestClass.someFunction.calls[0].args).toEqual(['foo']);
expect(TestClass.someFunction.calls[1].args).toEqual(['bar']);
expect(TestClass.someFunction.mostRecentCall.args).toEqual(['bar']);
expect(TestClass.someFunction.calls.first().args).toEqual(['foo']);
expect(TestClass.someFunction.calls.mostRecent().args).toEqual(['bar']);
});
it('should be possible to call through to the original method, or return a specific result', function() {
@@ -58,13 +57,13 @@ describe('Spies', function () {
}
};
env.spyOn(TestClass, 'someFunction').andCallThrough();
env.spyOn(TestClass, 'someFunction').and.callThrough();
var result = TestClass.someFunction('arg1', 'arg2');
expect(result).toEqual("return value from original function");
expect(originalFunctionWasCalled).toEqual(true);
expect(passedArgs).toEqual(['arg1', 'arg2']);
expect(passedObj).toEqual(TestClass);
expect(TestClass.someFunction.wasCalled).toEqual(true);
expect(TestClass.someFunction.calls.any()).toEqual(true);
});
it('should be possible to return a specific value', function() {
@@ -76,7 +75,7 @@ describe('Spies', function () {
}
};
env.spyOn(TestClass, 'someFunction').andReturn("some value");
env.spyOn(TestClass, 'someFunction').and.callReturn("some value");
originalFunctionWasCalled = false;
var result = TestClass.someFunction('arg1', 'arg2');
expect(result).toEqual("some value");
@@ -92,7 +91,7 @@ describe('Spies', function () {
}
};
env.spyOn(TestClass, 'someFunction').andThrow(new Error('fake error'));
env.spyOn(TestClass, 'someFunction').and.callThrow(new Error('fake error'));
var exception;
try {
TestClass.someFunction('arg1', 'arg2');
@@ -115,7 +114,7 @@ describe('Spies', function () {
}
};
env.spyOn(TestClass, 'someFunction').andCallFake(function() {
env.spyOn(TestClass, 'someFunction').and.callFake(function() {
fakeFunctionWasCalled = true;
passedArgs = Array.prototype.slice.call(arguments, 0);
passedObj = this;
@@ -128,38 +127,19 @@ describe('Spies', function () {
expect(fakeFunctionWasCalled).toEqual(true);
expect(passedArgs).toEqual(['arg1', 'arg2']);
expect(passedObj).toEqual(TestClass);
expect(TestClass.someFunction.wasCalled).toEqual(true);
expect(TestClass.someFunction.calls.any()).toEqual(true);
});
it('is torn down when env.removeAllSpies is called', function() {
var originalFunctionWasCalled = false,
env = new j$.Env(),
TestClass = {
someFunction: function() {
originalFunctionWasCalled = true;
}
};
env.spyOn(TestClass, 'someFunction');
TestClass.someFunction('foo');
expect(originalFunctionWasCalled).toEqual(false);
env.removeAllSpies();
TestClass.someFunction('foo');
expect(originalFunctionWasCalled).toEqual(true);
});
it('calls removeAllSpies during spec finish', function(done) {
it('removes all spies when env is executed', function(done) {
var env = new j$.Env(),
originalFoo = function() {},
testObj = {
foo: originalFoo
},
firstSpec = jasmine.createSpy('firstSpec').andCallFake(function() {
firstSpec = jasmine.createSpy('firstSpec').and.callFake(function() {
env.spyOn(testObj, 'foo');
}),
secondSpec = jasmine.createSpy('secondSpec').andCallFake(function() {
secondSpec = jasmine.createSpy('secondSpec').and.callFake(function() {
expect(testObj.foo).toBe(originalFoo);
});
env.describe('test suite', function() {
@@ -207,14 +187,14 @@ describe('Spies', function () {
it('should be able to reset a spy', function() {
var TestClass = { someFunction: function() {} };
env.spyOn(TestClass, 'someFunction');
spyOn(TestClass, 'someFunction');
expect(TestClass.someFunction).not.toHaveBeenCalled();
TestClass.someFunction();
expect(TestClass.someFunction).toHaveBeenCalled();
TestClass.someFunction.reset();
TestClass.someFunction.calls.reset();
expect(TestClass.someFunction).not.toHaveBeenCalled();
expect(TestClass.someFunction.callCount).toEqual(0);
expect(TestClass.someFunction.calls.count()).toEqual(0);
});
describe("createSpyObj", function() {
@@ -222,8 +202,8 @@ describe('Spies', function () {
var spyObj = j$.createSpyObj('BaseName', ['method1', 'method2']);
expect(spyObj).toEqual({ method1: jasmine.any(Function), method2: jasmine.any(Function)});
expect(spyObj.method1.identity).toEqual('BaseName.method1');
expect(spyObj.method2.identity).toEqual('BaseName.method2');
expect(spyObj.method1.and.identity()).toEqual('BaseName.method1');
expect(spyObj.method2.and.identity()).toEqual('BaseName.method2');
});
it("should throw if you do not pass an array argument", function() {