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:
committed by
Colin O'Byrne and JR Boyens
parent
18c30566bd
commit
3847557bbc
@@ -90,7 +90,7 @@ describe("ConsoleReporter", function() {
|
||||
reporter.jasmineStarted();
|
||||
reporter.specDone({status: "passed"});
|
||||
|
||||
timerSpy.elapsed.andReturn(1000);
|
||||
timerSpy.elapsed.and.callReturn(1000);
|
||||
|
||||
out.clear();
|
||||
reporter.jasmineDone();
|
||||
@@ -127,7 +127,7 @@ describe("ConsoleReporter", function() {
|
||||
|
||||
out.clear();
|
||||
|
||||
timerSpy.elapsed.andReturn(100);
|
||||
timerSpy.elapsed.and.callReturn(100);
|
||||
|
||||
reporter.jasmineDone();
|
||||
|
||||
|
||||
105
spec/core/CallTrackerSpec.js
Normal file
105
spec/core/CallTrackerSpec.js
Normal file
@@ -0,0 +1,105 @@
|
||||
describe("CallTracker", function() {
|
||||
it("tracks that it was called when executed", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
expect(callTracker.any()).toBe(false);
|
||||
|
||||
callTracker.track();
|
||||
|
||||
expect(callTracker.any()).toBe(true);
|
||||
});
|
||||
|
||||
it("tracks that number of times that it is executed", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
expect(callTracker.count()).toEqual(0);
|
||||
|
||||
callTracker.track();
|
||||
|
||||
expect(callTracker.count()).toEqual(1);
|
||||
});
|
||||
|
||||
it("tracks the params from each execution", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
callTracker.track({object: void 0, args: []});
|
||||
callTracker.track({object: {}, args: [0, "foo"]});
|
||||
|
||||
expect(callTracker.argsFor(0)).toEqual([]);
|
||||
|
||||
expect(callTracker.argsFor(1)).toEqual([0, "foo"]);
|
||||
});
|
||||
|
||||
it("returns any empty array when there was no call", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
expect(callTracker.argsFor(0)).toEqual([]);
|
||||
});
|
||||
|
||||
it("allows access for the arguments for all calls", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
callTracker.track({object: {}, args: []});
|
||||
callTracker.track({object: {}, args: [0, "foo"]});
|
||||
|
||||
expect(callTracker.allArgs()).toEqual([[], [0, "foo"]]);
|
||||
});
|
||||
|
||||
it("tracks the context and arguments for each call", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
callTracker.track({object: {}, args: []});
|
||||
callTracker.track({object: {}, args: [0, "foo"]});
|
||||
|
||||
expect(callTracker.all()[0]).toEqual({object: {}, args: []});
|
||||
|
||||
expect(callTracker.all()[1]).toEqual({object: {}, args: [0, "foo"]});
|
||||
});
|
||||
|
||||
it("simplifies access to the arguments for the last (most recent) call", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
callTracker.track();
|
||||
callTracker.track({object: {}, args: [0, "foo"]});
|
||||
|
||||
expect(callTracker.mostRecent()).toEqual({
|
||||
object: {},
|
||||
args: [0, "foo"]
|
||||
});
|
||||
});
|
||||
|
||||
it("returns a useful falsy value when there isn't a last (most recent) call", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
expect(callTracker.mostRecent()).toBeFalsy();
|
||||
});
|
||||
|
||||
it("simplifies access to the arguments for the first (oldest) call", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
callTracker.track({object: {}, args: [0, "foo"]});
|
||||
|
||||
expect(callTracker.first()).toEqual({object: {}, args: [0, "foo"]})
|
||||
});
|
||||
|
||||
it("returns a useful falsy value when there isn't a first (oldest) call", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
expect(callTracker.first()).toBeFalsy();
|
||||
});
|
||||
|
||||
|
||||
it("allows the tracking to be reset", function() {
|
||||
var callTracker = new j$.CallTracker();
|
||||
|
||||
callTracker.track();
|
||||
callTracker.track({object: {}, args: [0, "foo"]});
|
||||
callTracker.reset();
|
||||
|
||||
expect(callTracker.any()).toBe(false);
|
||||
expect(callTracker.count()).toEqual(0);
|
||||
expect(callTracker.argsFor(0)).toEqual([]);
|
||||
expect(callTracker.all()).toEqual([]);
|
||||
expect(callTracker.mostRecent()).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -31,7 +31,7 @@ describe("Clock", function() {
|
||||
it("returns an id for the delayed function", function() {
|
||||
var setTimeout = jasmine.createSpy('setTimeout'),
|
||||
scheduleId = 123,
|
||||
scheduleFunction = jasmine.createSpy('scheduleFunction').andReturn(scheduleId),
|
||||
scheduleFunction = jasmine.createSpy('scheduleFunction').and.callReturn(scheduleId),
|
||||
delayedFunctionScheduler = {scheduleFunction: scheduleFunction},
|
||||
global = { setTimeout: setTimeout },
|
||||
delayedFn = jasmine.createSpy('delayedFn'),
|
||||
@@ -100,7 +100,7 @@ describe("Clock", function() {
|
||||
it("returns an id for the delayed function", function() {
|
||||
var setInterval = jasmine.createSpy('setInterval'),
|
||||
scheduleId = 123,
|
||||
scheduleFunction = jasmine.createSpy('scheduleFunction').andReturn(scheduleId),
|
||||
scheduleFunction = jasmine.createSpy('scheduleFunction').and.callReturn(scheduleId),
|
||||
delayedFunctionScheduler = {scheduleFunction: scheduleFunction},
|
||||
global = { setInterval: setInterval },
|
||||
delayedFn = jasmine.createSpy('delayedFn'),
|
||||
@@ -239,25 +239,25 @@ describe("Clock (acceptance)", function() {
|
||||
clock.tick(50);
|
||||
|
||||
expect(recurring1).toHaveBeenCalledWith('some', 'other', 'args');
|
||||
expect(recurring1.callCount).toBe(1);
|
||||
expect(recurring1.calls.count()).toBe(1);
|
||||
expect(delayedFn2).not.toHaveBeenCalled();
|
||||
expect(delayedFn3).not.toHaveBeenCalled();
|
||||
|
||||
clock.tick(50);
|
||||
|
||||
expect(recurring1.callCount).toBe(2);
|
||||
expect(recurring1.calls.count()).toBe(2);
|
||||
expect(delayedFn2).toHaveBeenCalled();
|
||||
expect(delayedFn3).not.toHaveBeenCalled();
|
||||
|
||||
clock.tick(100);
|
||||
|
||||
expect(recurring1.callCount).toBe(4);
|
||||
expect(recurring1.calls.count()).toBe(4);
|
||||
expect(delayedFn3).toHaveBeenCalled();
|
||||
|
||||
clock.clearInterval(intervalId);
|
||||
clock.tick(50);
|
||||
|
||||
expect(recurring1.callCount).toBe(4);
|
||||
expect(recurring1.calls.count()).toBe(4);
|
||||
});
|
||||
|
||||
it("can clear a previously set timeout", function() {
|
||||
|
||||
@@ -72,15 +72,15 @@ describe("DelayedFunctionScheduler", function() {
|
||||
|
||||
scheduler.tick(20);
|
||||
|
||||
expect(fn.callCount).toBe(1);
|
||||
expect(fn.calls.count()).toBe(1);
|
||||
|
||||
scheduler.tick(40);
|
||||
|
||||
expect(fn.callCount).toBe(3);
|
||||
expect(fn.calls.count()).toBe(3);
|
||||
|
||||
scheduler.tick(21);
|
||||
|
||||
expect(fn.callCount).toBe(4);
|
||||
expect(fn.calls.count()).toBe(4);
|
||||
|
||||
});
|
||||
|
||||
@@ -158,7 +158,7 @@ describe("DelayedFunctionScheduler", function() {
|
||||
var scheduler = new j$.DelayedFunctionScheduler(),
|
||||
fn = jasmine.createSpy('fn'),
|
||||
recurringCallCount = 0,
|
||||
recurring = jasmine.createSpy('recurring').andCallFake(function() {
|
||||
recurring = jasmine.createSpy('recurring').and.callFake(function() {
|
||||
recurringCallCount++;
|
||||
if (recurringCallCount < 5) {
|
||||
expect(fn).not.toHaveBeenCalled();
|
||||
@@ -171,7 +171,7 @@ describe("DelayedFunctionScheduler", function() {
|
||||
scheduler.tick(60);
|
||||
|
||||
expect(recurring).toHaveBeenCalled();
|
||||
expect(recurring.callCount).toBe(6);
|
||||
expect(recurring.calls.count()).toBe(6);
|
||||
expect(fn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
@@ -225,11 +225,11 @@ describe("Env integration", function() {
|
||||
"specDone"
|
||||
]);
|
||||
|
||||
reporter.jasmineDone.andCallFake(function() {
|
||||
reporter.jasmineDone.and.callFake(function() {
|
||||
expect(reporter.jasmineStarted).toHaveBeenCalledWith({
|
||||
totalSpecsDefined: 3
|
||||
});
|
||||
var suiteResult = reporter.suiteStarted.calls[0].args[0];
|
||||
var suiteResult = reporter.suiteStarted.calls.first().args[0];
|
||||
expect(suiteResult.description).toEqual("A Suite");
|
||||
expect(reporter.jasmineDone).toHaveBeenCalled();
|
||||
|
||||
@@ -288,9 +288,9 @@ describe("Env integration", function() {
|
||||
"specDone"
|
||||
]);
|
||||
|
||||
reporter.jasmineDone.andCallFake(function() {
|
||||
var firstSpecResult = reporter.specDone.argsForCall[0][0],
|
||||
secondSpecResult = reporter.specDone.argsForCall[1][0];
|
||||
reporter.jasmineDone.and.callFake(function() {
|
||||
var firstSpecResult = reporter.specDone.calls.first().args[0],
|
||||
secondSpecResult = reporter.specDone.calls.mostRecent().args[0];
|
||||
|
||||
expect(firstSpecResult.status).toEqual("passed");
|
||||
expect(secondSpecResult.status).toEqual("failed");
|
||||
@@ -355,9 +355,9 @@ describe("Env integration", function() {
|
||||
"specDone"
|
||||
]);
|
||||
|
||||
reporter.jasmineDone.andCallFake(function() {
|
||||
var firstSpecResult = reporter.specDone.argsForCall[0][0],
|
||||
secondSpecResult = reporter.specDone.argsForCall[1][0];
|
||||
reporter.jasmineDone.and.callFake(function() {
|
||||
var firstSpecResult = reporter.specDone.calls.first().args[0],
|
||||
secondSpecResult = reporter.specDone.calls.mostRecent().args[0];
|
||||
|
||||
expect(firstSpecResult.status).toEqual("passed");
|
||||
expect(secondSpecResult.status).toEqual("failed");
|
||||
|
||||
@@ -16,7 +16,7 @@ describe("buildExpectationResult", function() {
|
||||
|
||||
it("delegates message formatting to the provided formatter if there was an Error", function() {
|
||||
var fakeError = {message: 'foo'},
|
||||
messageFormatter = jasmine.createSpy("exception message formatter").andReturn(fakeError.message);
|
||||
messageFormatter = jasmine.createSpy("exception message formatter").and.callReturn(fakeError.message);
|
||||
|
||||
var result = j$.buildExpectationResult(
|
||||
{
|
||||
@@ -31,7 +31,7 @@ describe("buildExpectationResult", function() {
|
||||
|
||||
it("delegates stack formatting to the provided formatter if there was an Error", function() {
|
||||
var fakeError = {stack: 'foo'},
|
||||
stackFormatter = jasmine.createSpy("stack formatter").andReturn(fakeError.stack);
|
||||
stackFormatter = jasmine.createSpy("stack formatter").and.callReturn(fakeError.stack);
|
||||
|
||||
var result = j$.buildExpectationResult(
|
||||
{
|
||||
|
||||
@@ -56,7 +56,7 @@ describe("Expectation", function() {
|
||||
|
||||
it("wraps matchers's compare functions, passing in matcher dependencies", function() {
|
||||
var fakeCompare = function() { return { pass: true }; },
|
||||
matcherFactory = jasmine.createSpy("matcher").andReturn({ compare: fakeCompare }),
|
||||
matcherFactory = jasmine.createSpy("matcher").and.callReturn({ compare: fakeCompare }),
|
||||
matchers = {
|
||||
toFoo: matcherFactory
|
||||
},
|
||||
@@ -80,7 +80,7 @@ describe("Expectation", function() {
|
||||
});
|
||||
|
||||
it("wraps matchers's compare functions, passing the actual and expected", function() {
|
||||
var fakeCompare = jasmine.createSpy('fake-compare').andReturn({pass: true}),
|
||||
var fakeCompare = jasmine.createSpy('fake-compare').and.callReturn({pass: true}),
|
||||
matchers = {
|
||||
toFoo: function() {
|
||||
return {
|
||||
|
||||
@@ -196,7 +196,7 @@ describe("JsApiReporter", function() {
|
||||
timer: timerSpy
|
||||
});
|
||||
|
||||
timerSpy.elapsed.andReturn(1000);
|
||||
timerSpy.elapsed.and.callReturn(1000);
|
||||
reporter.jasmineDone();
|
||||
expect(reporter.executionTime()).toEqual(1000);
|
||||
});
|
||||
|
||||
@@ -104,13 +104,14 @@ describe("j$.pp", function () {
|
||||
|
||||
it("should stringify spy objects properly", function() {
|
||||
var TestObject = {
|
||||
someFunction: function() {
|
||||
}
|
||||
};
|
||||
spyOn(TestObject, 'someFunction');
|
||||
someFunction: function() {}
|
||||
},
|
||||
env = new j$.Env();
|
||||
|
||||
env.spyOn(TestObject, 'someFunction');
|
||||
expect(j$.pp(TestObject.someFunction)).toEqual("spy on someFunction");
|
||||
|
||||
expect(j$.pp(jasmine.createSpy("something"))).toEqual("spy on something");
|
||||
expect(j$.pp(j$.createSpy("something"))).toEqual("spy on something");
|
||||
});
|
||||
|
||||
it("should stringify objects that implement jasmineToString", function () {
|
||||
|
||||
@@ -7,10 +7,10 @@ describe("QueueRunner", function() {
|
||||
queueRunner = new j$.QueueRunner({
|
||||
fns: [fn1, fn2]
|
||||
});
|
||||
fn1.andCallFake(function() {
|
||||
fn1.and.callFake(function() {
|
||||
calls.push('fn1');
|
||||
});
|
||||
fn2.andCallFake(function() {
|
||||
fn2.and.callFake(function() {
|
||||
calls.push('fn2');
|
||||
});
|
||||
|
||||
@@ -21,7 +21,7 @@ describe("QueueRunner", function() {
|
||||
|
||||
it("supports asynchronous functions, only advancing to next function after a done() callback", function() {
|
||||
//TODO: it would be nice if spy arity could match the fake, so we could do something like:
|
||||
//createSpy('asyncfn').andCallFake(function(done) {});
|
||||
//createSpy('asyncfn').and.callFake(function(done) {});
|
||||
|
||||
var onComplete = jasmine.createSpy('onComplete'),
|
||||
beforeCallback = jasmine.createSpy('beforeCallback'),
|
||||
@@ -138,6 +138,8 @@ describe("QueueRunner", function() {
|
||||
onComplete: completeCallback
|
||||
});
|
||||
|
||||
clearStack.and.callFake(function(fn) { fn(); });
|
||||
|
||||
queueRunner.execute();
|
||||
expect(afterFn).toHaveBeenCalled();
|
||||
expect(clearStack).toHaveBeenCalledWith(completeCallback);
|
||||
|
||||
@@ -54,7 +54,7 @@ describe("Spec", function() {
|
||||
it("should call the start callback on execution but before any befores are called", function() {
|
||||
var fakeQueueRunner = jasmine.createSpy('fakeQueueRunner'),
|
||||
beforesWereCalled = false,
|
||||
startCallback = jasmine.createSpy('start-callback').andCallFake(function() {
|
||||
startCallback = jasmine.createSpy('start-callback').and.callFake(function() {
|
||||
expect(beforesWereCalled).toBe(false);
|
||||
}),
|
||||
spec = new j$.Spec({
|
||||
@@ -77,7 +77,7 @@ describe("Spec", function() {
|
||||
var fakeQueueRunner = jasmine.createSpy('fakeQueueRunner'),
|
||||
before = jasmine.createSpy('before'),
|
||||
after = jasmine.createSpy('after'),
|
||||
fn = jasmine.createSpy('test body').andCallFake(function() {
|
||||
fn = jasmine.createSpy('test body').and.callFake(function() {
|
||||
expect(before).toHaveBeenCalled();
|
||||
expect(after).not.toHaveBeenCalled();
|
||||
}),
|
||||
@@ -94,7 +94,7 @@ describe("Spec", function() {
|
||||
|
||||
spec.execute();
|
||||
|
||||
var allSpecFns = fakeQueueRunner.mostRecentCall.args[0].fns;
|
||||
var allSpecFns = fakeQueueRunner.calls.mostRecent().args[0].fns;
|
||||
expect(allSpecFns).toEqual([before, fn, after]);
|
||||
});
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
82
spec/core/SpyStrategySpec.js
Normal file
82
spec/core/SpyStrategySpec.js
Normal file
@@ -0,0 +1,82 @@
|
||||
describe("SpyStrategy", function() {
|
||||
|
||||
it("defaults its name to unknown", function() {
|
||||
var spyStrategy = new j$.SpyStrategy();
|
||||
|
||||
expect(spyStrategy.identity()).toEqual("unknown");
|
||||
});
|
||||
|
||||
it("takes a name", function() {
|
||||
var spyStrategy = new j$.SpyStrategy({name: "foo"});
|
||||
|
||||
expect(spyStrategy.identity()).toEqual("foo");
|
||||
});
|
||||
|
||||
it("stubs an original function, if provided", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
spyStrategy = new j$.SpyStrategy({fn: originalFn});
|
||||
|
||||
spyStrategy.exec();
|
||||
|
||||
expect(originalFn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows an original function to be called, passed through the params and returns it's value", function() {
|
||||
var originalFn = jasmine.createSpy("original").and.callReturn(42),
|
||||
spyStrategy = new j$.SpyStrategy({fn: originalFn}),
|
||||
returnValue;
|
||||
|
||||
spyStrategy.callThrough();
|
||||
returnValue = spyStrategy.exec("foo");
|
||||
|
||||
expect(originalFn).toHaveBeenCalled();
|
||||
expect(originalFn.calls.mostRecent().args).toEqual(["foo"]);
|
||||
expect(returnValue).toEqual(42);
|
||||
});
|
||||
|
||||
it("can return a specified value when executed", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
spyStrategy = new j$.SpyStrategy({fn: originalFn}),
|
||||
returnValue;
|
||||
|
||||
spyStrategy.callReturn(17);
|
||||
returnValue = spyStrategy.exec();
|
||||
|
||||
expect(originalFn).not.toHaveBeenCalled();
|
||||
expect(returnValue).toEqual(17);
|
||||
});
|
||||
|
||||
it("allows an exception to be thrown when executed", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
spyStrategy = new j$.SpyStrategy({fn: originalFn});
|
||||
|
||||
spyStrategy.callThrow("bar");
|
||||
|
||||
expect(function() { spyStrategy.exec(); }).toThrow("bar");
|
||||
expect(originalFn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows a fake function to be called instead", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
fakeFn = jasmine.createSpy("fake").and.callReturn(67),
|
||||
spyStrategy = new j$.SpyStrategy({fn: originalFn}),
|
||||
returnValue;
|
||||
|
||||
spyStrategy.callFake(fakeFn);
|
||||
returnValue = spyStrategy.exec();
|
||||
|
||||
expect(originalFn).not.toHaveBeenCalled();
|
||||
expect(returnValue).toEqual(67);
|
||||
});
|
||||
|
||||
it("returns the spy after changing the strategy", function(){
|
||||
var spy = {},
|
||||
spyFn = jasmine.createSpy('spyFn').and.callReturn(spy),
|
||||
spyStrategy = new j$.SpyStrategy({getSpy: spyFn});
|
||||
|
||||
expect(spyStrategy.callThrough()).toBe(spy);
|
||||
expect(spyStrategy.callReturn()).toBe(spy);
|
||||
expect(spyStrategy.callThrow()).toBe(spy);
|
||||
expect(spyStrategy.callFake()).toBe(spy);
|
||||
});
|
||||
});
|
||||
@@ -159,7 +159,7 @@ describe("Suite", function() {
|
||||
|
||||
parentSuite.execute(parentSuiteDone);
|
||||
|
||||
var parentSuiteFns = fakeQueueRunnerForParent.mostRecentCall.args[0].fns;
|
||||
var parentSuiteFns = fakeQueueRunnerForParent.calls.mostRecent().args[0].fns;
|
||||
|
||||
parentSuiteFns[0]();
|
||||
expect(fakeSpec1.execute).toHaveBeenCalled();
|
||||
|
||||
@@ -3,10 +3,10 @@ describe("Timer", function() {
|
||||
var fakeNow = jasmine.createSpy('fake Date.now'),
|
||||
timer = new j$.Timer({now: fakeNow});
|
||||
|
||||
fakeNow.andReturn(100);
|
||||
fakeNow.and.callReturn(100);
|
||||
timer.start();
|
||||
|
||||
fakeNow.andReturn(200);
|
||||
fakeNow.and.callReturn(200);
|
||||
|
||||
expect(timer.elapsed()).toEqual(100);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
describe("toContain", function() {
|
||||
it("delegates to j$.matchersUtil.contains", function() {
|
||||
var util = {
|
||||
contains: jasmine.createSpy('delegated-contains').andReturn(true)
|
||||
contains: jasmine.createSpy('delegated-contains').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toContain(util);
|
||||
|
||||
@@ -12,7 +12,7 @@ describe("toContain", function() {
|
||||
|
||||
it("delegates to j$.matchersUtil.contains, passing in equality testers if present", function() {
|
||||
var util = {
|
||||
contains: jasmine.createSpy('delegated-contains').andReturn(true)
|
||||
contains: jasmine.createSpy('delegated-contains').and.callReturn(true)
|
||||
},
|
||||
customEqualityTesters = ['a', 'b'],
|
||||
matcher = j$.matchers.toContain(util, customEqualityTesters);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
describe("toEqual", function() {
|
||||
it("delegates to equals function", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equals').andReturn(true)
|
||||
equals: jasmine.createSpy('delegated-equals').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toEqual(util),
|
||||
result;
|
||||
@@ -14,7 +14,7 @@ describe("toEqual", function() {
|
||||
|
||||
it("delegates custom equality testers, if present", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equals').andReturn(true)
|
||||
equals: jasmine.createSpy('delegated-equals').and.callReturn(true)
|
||||
},
|
||||
customEqualityTesters = ['a', 'b'],
|
||||
matcher = j$.matchers.toEqual(util, customEqualityTesters),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
describe("toHaveBeenCalled", function() {
|
||||
it("passes when the actual was called, with a custom .not fail message", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalled(),
|
||||
calledSpy = jasmine.createSpy('called-spy'),
|
||||
calledSpy = j$.createSpy('called-spy'),
|
||||
result;
|
||||
|
||||
calledSpy();
|
||||
@@ -13,7 +13,7 @@ describe("toHaveBeenCalled", function() {
|
||||
|
||||
it("fails when the actual was not called", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalled(),
|
||||
uncalledSpy = jasmine.createSpy('uncalled spy');
|
||||
uncalledSpy = j$.createSpy('uncalled spy');
|
||||
|
||||
result = matcher.compare(uncalledSpy);
|
||||
expect(result.pass).toBe(false);
|
||||
@@ -28,14 +28,14 @@ describe("toHaveBeenCalled", function() {
|
||||
|
||||
it("throws an exception when invoked with any arguments", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalled(),
|
||||
spy = jasmine.createSpy('sample spy');
|
||||
spy = j$.createSpy('sample spy');
|
||||
|
||||
expect(function() { matcher.compare(spy, 'foo') }).toThrow(new Error("toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith"));
|
||||
});
|
||||
|
||||
it("has a custom message on failure", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalled(),
|
||||
spy = jasmine.createSpy('sample-spy'),
|
||||
spy = j$.createSpy('sample-spy'),
|
||||
result;
|
||||
|
||||
result = matcher.compare(spy);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
describe("toHaveBeenCalledWith", function() {
|
||||
it("passes when the actual was called with matching parameters", function() {
|
||||
var util = {
|
||||
contains: jasmine.createSpy('delegated-contains').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toHaveBeenCalledWith(util),
|
||||
calledSpy = jasmine.createSpy('called-spy'),
|
||||
result;
|
||||
contains: jasmine.createSpy('delegated-contains').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toHaveBeenCalledWith(util),
|
||||
calledSpy = j$.createSpy('called-spy'),
|
||||
result;
|
||||
|
||||
calledSpy('a', 'b');
|
||||
result = matcher.compare(calledSpy, 'a', 'b');
|
||||
@@ -15,11 +15,11 @@ describe("toHaveBeenCalledWith", function() {
|
||||
|
||||
it("fails when the actual was not called", function() {
|
||||
var util = {
|
||||
contains: jasmine.createSpy('delegated-contains').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toHaveBeenCalledWith(util),
|
||||
uncalledSpy = jasmine.createSpy('uncalled spy'),
|
||||
result;
|
||||
contains: jasmine.createSpy('delegated-contains').and.callReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toHaveBeenCalledWith(util),
|
||||
uncalledSpy = j$.createSpy('uncalled spy'),
|
||||
result;
|
||||
|
||||
result = matcher.compare(uncalledSpy);
|
||||
expect(result.pass).toBe(false);
|
||||
@@ -27,11 +27,11 @@ describe("toHaveBeenCalledWith", function() {
|
||||
|
||||
it("fails when the actual was called with different parameters", function() {
|
||||
var util = {
|
||||
contains: jasmine.createSpy('delegated-contains').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toHaveBeenCalledWith(util),
|
||||
calledSpy = jasmine.createSpy('called spy'),
|
||||
result;
|
||||
contains: jasmine.createSpy('delegated-contains').and.callReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toHaveBeenCalledWith(util),
|
||||
calledSpy = j$.createSpy('called spy'),
|
||||
result;
|
||||
|
||||
calledSpy('a');
|
||||
result = matcher.compare(calledSpy, 'a', 'b');
|
||||
@@ -41,15 +41,15 @@ describe("toHaveBeenCalledWith", function() {
|
||||
|
||||
it("throws an exception when the actual is not a spy", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalledWith(),
|
||||
fn = function() {};
|
||||
fn = function() {};
|
||||
|
||||
expect(function() { matcher.compare(fn) }).toThrow(new Error("Expected a spy, but got Function."));
|
||||
});
|
||||
|
||||
it("has a custom message on failure", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalledWith(),
|
||||
spy = jasmine.createSpy('sample-spy'),
|
||||
messages = matcher.message(spy);
|
||||
spy = j$.createSpy('sample-spy'),
|
||||
messages = matcher.message(spy);
|
||||
|
||||
expect(messages.affirmative).toEqual("Expected spy sample-spy to have been called.")
|
||||
expect(messages.negative).toEqual("Expected spy sample-spy not to have been called.")
|
||||
|
||||
@@ -144,7 +144,7 @@ describe("toThrowError", function() {
|
||||
|
||||
it("passes if thrown is an Error and the expected the same Error", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(true)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
@@ -160,7 +160,7 @@ describe("toThrowError", function() {
|
||||
|
||||
it("passes if thrown is a custom error that takes arguments and the expected is the same error", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(true)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
CustomError = function CustomError(arg) { arg.x },
|
||||
@@ -180,7 +180,7 @@ describe("toThrowError", function() {
|
||||
|
||||
it("fails if thrown is an Error and the expected is a different Error", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(false)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
@@ -196,7 +196,7 @@ describe("toThrowError", function() {
|
||||
|
||||
it("passes if thrown is a type of Error and it is equal to the expected Error and message", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(true)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
@@ -212,7 +212,7 @@ describe("toThrowError", function() {
|
||||
|
||||
it("passes if thrown is a custom error that takes arguments and it is equal to the expected custom error and message", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(true)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
CustomError = function CustomError(arg) { this.message = arg.message },
|
||||
@@ -232,7 +232,7 @@ describe("toThrowError", function() {
|
||||
|
||||
it("fails if thrown is a type of Error and the expected is a different Error", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(false)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
@@ -248,7 +248,7 @@ describe("toThrowError", function() {
|
||||
|
||||
it("passes if thrown is a type of Error and has the same type as the expected Error and the message matches the exepcted message", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(true)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
@@ -264,7 +264,7 @@ describe("toThrowError", function() {
|
||||
|
||||
it("fails if thrown is a type of Error and the expected is a different Error", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(false)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
|
||||
@@ -22,7 +22,7 @@ describe("toThrow", function() {
|
||||
|
||||
it("passes if it throws but there is no expected", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(true)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
@@ -50,7 +50,7 @@ describe("toThrow", function() {
|
||||
|
||||
it("passes if what is thrown is equivalent to what is expected", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(true)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
@@ -66,7 +66,7 @@ describe("toThrow", function() {
|
||||
|
||||
it("fails if what is thrown is not equivalent to what is expected", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(false)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
@@ -82,7 +82,7 @@ describe("toThrow", function() {
|
||||
|
||||
it("fails if what is thrown is not equivalent to undefined", function() {
|
||||
var util = {
|
||||
equals: jasmine.createSpy('delegated-equal').andReturn(false)
|
||||
equals: jasmine.createSpy('delegated-equal').and.callReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
|
||||
@@ -148,7 +148,7 @@ describe("New HtmlReporter", function() {
|
||||
|
||||
reporter.jasmineStarted({});
|
||||
|
||||
timer.elapsed.andReturn(100);
|
||||
timer.elapsed.and.callReturn(100);
|
||||
reporter.jasmineDone();
|
||||
|
||||
var duration = container.querySelector(".banner .duration");
|
||||
|
||||
Reference in New Issue
Block a user