Moved createSpy to env so it can be stateful

This commit is contained in:
Steve Gravrock
2018-01-09 10:16:02 -08:00
parent 298b5ba127
commit 6f119c4e5a
12 changed files with 147 additions and 97 deletions

View File

@@ -305,18 +305,6 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return new j$.ArrayWithExactContents(sample);
};
/**
* Create a bare {@link Spy} object. This won't be installed anywhere and will not have any implementation behind it.
* @name jasmine.createSpy
* @function
* @param {String} [name] - Name to give the spy. This will be displayed in failure messages.
* @param {Function} [originalFn] - Function to act as the real implementation.
* @return {Spy}
*/
j$.createSpy = function(name, originalFn) {
return j$.Spy(name, originalFn);
};
j$.isSpy = function(putativeSpy) {
if (!putativeSpy) {
return false;
@@ -1093,12 +1081,17 @@ getJasmineRequireObj().Env = function(j$) {
reporter.clearReporters();
};
var spyRegistry = new j$.SpyRegistry({currentSpies: function() {
if(!currentRunnable()) {
throw new Error('Spies must be created in a before function or a spec');
var spyRegistry = new j$.SpyRegistry({
currentSpies: function() {
if(!currentRunnable()) {
throw new Error('Spies must be created in a before function or a spec');
}
return runnableResources[currentRunnable().id].spies;
},
createSpy: function(name, originalFn) {
return self.createSpy(name, originalFn);
}
return runnableResources[currentRunnable().id].spies;
}});
});
this.allowRespy = function(allow){
spyRegistry.allowRespy(allow);
@@ -1112,6 +1105,10 @@ getJasmineRequireObj().Env = function(j$) {
return spyRegistry.spyOnProperty.apply(spyRegistry, arguments);
};
this.createSpy = function(name, originalFn) {
return j$.Spy(name, originalFn);
};
this.createSpyObj = function(baseName, methodNames) {
var baseNameIsCollection = j$.isObject_(baseName) || j$.isArray_(baseName);
@@ -1125,13 +1122,13 @@ getJasmineRequireObj().Env = function(j$) {
if (j$.isArray_(methodNames)) {
for (var i = 0; i < methodNames.length; i++) {
obj[methodNames[i]] = j$.createSpy(baseName + '.' + methodNames[i]);
obj[methodNames[i]] = self.createSpy(baseName + '.' + methodNames[i]);
spiesWereSet = true;
}
} else if (j$.isObject_(methodNames)) {
for (var key in methodNames) {
if (methodNames.hasOwnProperty(key)) {
obj[key] = j$.createSpy(baseName + '.' + key);
obj[key] = self.createSpy(baseName + '.' + key);
obj[key].and.returnValue(methodNames[key]);
spiesWereSet = true;
}
@@ -4906,6 +4903,18 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.clock;
};
/**
* Create a bare {@link Spy} object. This won't be installed anywhere and will not have any implementation behind it.
* @name jasmine.createSpy
* @function
* @param {String} [name] - Name to give the spy. This will be displayed in failure messages.
* @param {Function} [originalFn] - Function to act as the real implementation.
* @return {Spy}
*/
jasmine.createSpy = function(name, originalFn) {
return env.createSpy(name, originalFn);
};
/**
* Create an object with multiple {@link Spy}s as its members.
* @name jasmine.createSpyObj
@@ -5090,6 +5099,7 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
function SpyRegistry(options) {
options = options || {};
var createSpy = options.createSpy;
var currentSpies = options.currentSpies || function() { return []; };
this.allowRespy = function(allow){
@@ -5125,7 +5135,7 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
}
var originalMethod = obj[methodName],
spiedMethod = j$.createSpy(methodName, originalMethod),
spiedMethod = createSpy(methodName, originalMethod),
restoreStrategy;
if (Object.prototype.hasOwnProperty.call(obj, methodName)) {
@@ -5180,7 +5190,7 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
}
var originalDescriptor = j$.util.clone(descriptor),
spy = j$.createSpy(propertyName, descriptor[accessType]),
spy = createSpy(propertyName, descriptor[accessType]),
restoreStrategy;
if (Object.prototype.hasOwnProperty.call(obj, propertyName)) {

View File

@@ -255,12 +255,17 @@ describe("jasmineUnderTest.pp", function () {
},
env = new jasmineUnderTest.Env();
var spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() {return [];}});
var spyRegistry = new jasmineUnderTest.SpyRegistry({
currentSpies: function() {return [];},
createSpy: function(name, originalFn) {
return jasmineUnderTest.Spy(name, originalFn);
}
});
spyRegistry.spyOn(TestObject, 'someFunction');
expect(jasmineUnderTest.pp(TestObject.someFunction)).toEqual("spy on someFunction");
expect(jasmineUnderTest.pp(jasmineUnderTest.createSpy("something"))).toEqual("spy on something");
expect(jasmineUnderTest.pp(env.createSpy("something"))).toEqual("spy on something");
});
it("should stringify objects that implement jasmineToString", function () {

View File

@@ -1,7 +1,11 @@
describe("SpyRegistry", function() {
function createSpy(name, originalFn) {
return jasmineUnderTest.Spy(name, originalFn);
}
describe("#spyOn", function() {
it("checks for the existence of the object", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry();
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy});
expect(function() {
spyRegistry.spyOn(void 0, 'pants');
}).toThrowError(/could not find an object/);
@@ -43,7 +47,10 @@ describe("SpyRegistry", function() {
it("checks if it has already been spied upon", function() {
var spies = [],
spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() { return spies; }}),
spyRegistry = new jasmineUnderTest.SpyRegistry({
currentSpies: function() { return spies; },
createSpy: createSpy
}),
subject = { spiedFunc: function() {} };
spyRegistry.spyOn(subject, 'spiedFunc');
@@ -81,7 +88,7 @@ describe("SpyRegistry", function() {
it("overrides the method on the object and returns the spy", function() {
var originalFunctionWasCalled = false,
spyRegistry = new jasmineUnderTest.SpyRegistry(),
spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
subject = { spiedFunc: function() { originalFunctionWasCalled = true; } };
var spy = spyRegistry.spyOn(subject, 'spiedFunc');
@@ -131,7 +138,7 @@ describe("SpyRegistry", function() {
});
it("checks if it has already been spied upon", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry(),
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
subject = {};
Object.defineProperty(subject, 'spiedProp', {
@@ -170,7 +177,7 @@ describe("SpyRegistry", function() {
});
it("overrides the property getter on the object and returns the spy", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry(),
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
subject = {},
returnValue = 1;
@@ -189,7 +196,7 @@ describe("SpyRegistry", function() {
});
it("overrides the property setter on the object and returns the spy", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry(),
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
subject = {},
returnValue = 1;
@@ -210,7 +217,10 @@ describe("SpyRegistry", function() {
describe("#clearSpies", function() {
it("restores the original functions on the spied-upon objects", function() {
var spies = [],
spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() { return spies; }}),
spyRegistry = new jasmineUnderTest.SpyRegistry({
currentSpies: function() { return spies; },
createSpy: createSpy
}),
originalFunction = function() {},
subject = { spiedFunc: originalFunction };
@@ -222,7 +232,10 @@ describe("SpyRegistry", function() {
it("restores the original functions, even when that spy has been replace and re-spied upon", function() {
var spies = [],
spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() { return spies; }}),
spyRegistry = new jasmineUnderTest.SpyRegistry({
currentSpies: function() { return spies; },
createSpy: createSpy
}),
originalFunction = function() {},
subject = { spiedFunc: originalFunction };
@@ -241,7 +254,10 @@ describe("SpyRegistry", function() {
it("does not add a property that the spied-upon object didn't originally have", function() {
var spies = [],
spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() { return spies; }}),
spyRegistry = new jasmineUnderTest.SpyRegistry({
currentSpies: function() { return spies; },
createSpy: createSpy
}),
originalFunction = function() {},
subjectParent = {spiedFunc: originalFunction};
@@ -258,7 +274,10 @@ describe("SpyRegistry", function() {
it("restores the original function when it\'s inherited and cannot be deleted", function() {
var spies = [],
spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() { return spies; }}),
spyRegistry = new jasmineUnderTest.SpyRegistry({
currentSpies: function() { return spies; },
createSpy: createSpy
}),
originalFunction = function() {},
subjectParent = {spiedFunc: originalFunction};
@@ -280,7 +299,10 @@ describe("SpyRegistry", function() {
describe('spying on properties', function() {
it("restores the original properties on the spied-upon objects", function() {
var spies = [],
spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() { return spies; }}),
spyRegistry = new jasmineUnderTest.SpyRegistry({
currentSpies: function() { return spies; },
createSpy: createSpy
}),
originalReturn = 1,
subject = {};
@@ -297,7 +319,10 @@ describe("SpyRegistry", function() {
it("does not add a property that the spied-upon object didn't originally have", function() {
var spies = [],
spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() { return spies; }}),
spyRegistry = new jasmineUnderTest.SpyRegistry({
currentSpies: function() { return spies; },
createSpy: createSpy
}),
originalReturn = 1,
subjectParent = {};

View File

@@ -15,7 +15,7 @@ describe('Spies', function () {
});
it("preserves the properties of the spied function", function() {
var spy = jasmineUnderTest.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
expect(spy.bob).toEqual("test");
});
@@ -24,19 +24,19 @@ describe('Spies', function () {
TestClass.prototype.someFunction.and = "turkey";
expect(function() {
jasmineUnderTest.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
}).toThrowError("Jasmine spies would overwrite the 'and' and 'calls' properties on the object being spied upon");
});
it("adds a spyStrategy and callTracker to the spy", function() {
var spy = jasmineUnderTest.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
expect(spy.and).toEqual(jasmine.any(jasmineUnderTest.SpyStrategy));
expect(spy.calls).toEqual(jasmine.any(jasmineUnderTest.CallTracker));
});
it("tracks the argument of calls", function () {
var spy = jasmineUnderTest.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
var trackSpy = spyOn(spy.calls, "track");
spy("arg");
@@ -45,7 +45,7 @@ describe('Spies', function () {
});
it("tracks the context of calls", function () {
var spy = jasmineUnderTest.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
var trackSpy = spyOn(spy.calls, "track");
var contextObject = { spyMethod: spy };
@@ -55,7 +55,7 @@ describe('Spies', function () {
});
it("tracks the return value of calls", function () {
var spy = jasmineUnderTest.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
var trackSpy = spyOn(spy.calls, "track");
spy.and.returnValue("return value");
@@ -77,7 +77,7 @@ describe('Spies', function () {
for (var arity = 0; arity < functions.length; arity++) {
var someFunction = functions[arity],
spy = jasmineUnderTest.createSpy(someFunction.name, someFunction);
spy = env.createSpy(someFunction.name, someFunction);
expect(spy.length).toEqual(arity);
}
@@ -132,7 +132,7 @@ describe('Spies', function () {
});
it("can use different strategies for different arguments", function() {
var spy = jasmineUnderTest.createSpy('foo');
var spy = env.createSpy('foo');
spy.and.returnValue(42);
spy.withArgs('baz', 'grault').and.returnValue(-1);
spy.withArgs('thud').and.returnValue('bob');
@@ -144,7 +144,7 @@ describe('Spies', function () {
});
it("uses custom equality testers when selecting a strategy", function() {
var spy = jasmineUnderTest.createSpy('foo');
var spy = env.createSpy('foo');
spy.and.returnValue(42);
spy.withArgs(jasmineUnderTest.any(String)).and.returnValue(-1);
@@ -153,7 +153,7 @@ describe('Spies', function () {
});
it("can reconfigure an argument-specific strategy", function() {
var spy = jasmineUnderTest.createSpy('foo');
var spy = env.createSpy('foo');
spy.withArgs('foo').and.returnValue(42);
spy.withArgs('foo').and.returnValue(17);
expect(spy('foo')).toEqual(17);
@@ -161,14 +161,14 @@ describe('Spies', function () {
describe("When withArgs is used without a base strategy", function() {
it("uses the matching strategy", function() {
var spy = jasmineUnderTest.createSpy('foo');
var spy = env.createSpy('foo');
spy.withArgs('baz').and.returnValue(-1);
expect(spy('baz')).toEqual(-1);
});
it("throws if the args don't match", function() {
var spy = jasmineUnderTest.createSpy('foo');
var spy = env.createSpy('foo');
spy.withArgs('bar').and.returnValue(-1);
expect(function() { spy('baz', {qux: 42}); }).toThrowError('Spy \'foo\' receieved a call with arguments [ \'baz\', Object({ qux: 42 }) ] but all configured strategies specify other arguments.');

View File

@@ -2,14 +2,14 @@ describe("toHaveBeenCalledBefore", function() {
it("throws an exception when the actual is not a spy", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
fn = function() {},
secondSpy = jasmineUnderTest.createSpy('second spy');
secondSpy = new jasmineUnderTest.Env().createSpy('second spy');
expect(function() { matcher.compare(fn, secondSpy) }).toThrowError(Error, /Expected a spy, but got Function./);
});
it("throws an exception when the expected is not a spy", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = jasmineUnderTest.createSpy('first spy'),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
fn = function() {};
expect(function() { matcher.compare(firstSpy, fn) }).toThrowError(Error, /Expected a spy, but got Function./);
@@ -17,8 +17,8 @@ describe("toHaveBeenCalledBefore", function() {
it("fails when the actual was not called", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = jasmineUnderTest.createSpy('first spy'),
secondSpy = jasmineUnderTest.createSpy('second spy');
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy');
secondSpy();
@@ -29,8 +29,8 @@ describe("toHaveBeenCalledBefore", function() {
it("fails when the expected was not called", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = jasmineUnderTest.createSpy('first spy'),
secondSpy = jasmineUnderTest.createSpy('second spy');
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy');
firstSpy();
@@ -41,8 +41,8 @@ describe("toHaveBeenCalledBefore", function() {
it("fails when the actual is called after the expected", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = jasmineUnderTest.createSpy('first spy'),
secondSpy = jasmineUnderTest.createSpy('second spy'),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy'),
result;
secondSpy();
@@ -55,8 +55,8 @@ describe("toHaveBeenCalledBefore", function() {
it("fails when the actual is called before and after the expected", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = jasmineUnderTest.createSpy('first spy'),
secondSpy = jasmineUnderTest.createSpy('second spy'),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy'),
result;
firstSpy();
@@ -70,8 +70,8 @@ describe("toHaveBeenCalledBefore", function() {
it("fails when the expected is called before and after the actual", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = jasmineUnderTest.createSpy('first spy'),
secondSpy = jasmineUnderTest.createSpy('second spy'),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy'),
result;
secondSpy();
@@ -85,8 +85,8 @@ describe("toHaveBeenCalledBefore", function() {
it("passes when the actual is called before the expected", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = jasmineUnderTest.createSpy('first spy'),
secondSpy = jasmineUnderTest.createSpy('second spy'),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy'),
result;
firstSpy();

View File

@@ -1,7 +1,7 @@
describe("toHaveBeenCalled", function() {
it("passes when the actual was called, with a custom .not fail message", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled(),
calledSpy = jasmineUnderTest.createSpy('called-spy'),
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy'),
result;
calledSpy();
@@ -13,7 +13,7 @@ describe("toHaveBeenCalled", function() {
it("fails when the actual was not called", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled(),
uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'),
uncalledSpy = new jasmineUnderTest.Env().createSpy('uncalled spy'),
result;
result = matcher.compare(uncalledSpy);
@@ -29,14 +29,14 @@ describe("toHaveBeenCalled", function() {
it("throws an exception when invoked with any arguments", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled(),
spy = jasmineUnderTest.createSpy('sample spy');
spy = new jasmineUnderTest.Env().createSpy('sample spy');
expect(function() { matcher.compare(spy, 'foo') }).toThrowError(Error, /Does not take arguments, use toHaveBeenCalledWith/);
});
it("has a custom message on failure", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled(),
spy = jasmineUnderTest.createSpy('sample-spy'),
spy = new jasmineUnderTest.Env().createSpy('sample-spy'),
result;
result = matcher.compare(spy);

View File

@@ -1,14 +1,14 @@
describe("toHaveBeenCalledTimes", function() {
it("passes when the actual 0 matches the expected 0 ", function () {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
calledSpy = jasmineUnderTest.createSpy('called-spy'),
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy'),
result;
result = matcher.compare(calledSpy, 0);
expect(result.pass).toBeTruthy();
});
it("passes when the actual matches the expected", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
calledSpy = jasmineUnderTest.createSpy('called-spy'),
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy'),
result;
calledSpy();
@@ -18,7 +18,7 @@ describe("toHaveBeenCalledTimes", function() {
it("fails when expected numbers is not supplied", function(){
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
spy = jasmineUnderTest.createSpy('spy'),
spy = new jasmineUnderTest.Env().createSpy('spy'),
result;
spy();
@@ -29,7 +29,7 @@ describe("toHaveBeenCalledTimes", function() {
it("fails when the actual was called less than the expected", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'),
uncalledSpy = new jasmineUnderTest.Env().createSpy('uncalled spy'),
result;
result = matcher.compare(uncalledSpy, 2);
@@ -38,7 +38,7 @@ describe("toHaveBeenCalledTimes", function() {
it("fails when the actual was called more than expected", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'),
uncalledSpy = new jasmineUnderTest.Env().createSpy('uncalled spy'),
result;
uncalledSpy();
@@ -59,7 +59,7 @@ describe("toHaveBeenCalledTimes", function() {
it("has a custom message on failure that tells it was called only once", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
spy = jasmineUnderTest.createSpy('sample-spy'),
spy = new jasmineUnderTest.Env().createSpy('sample-spy'),
result;
spy();
spy();
@@ -72,7 +72,7 @@ describe("toHaveBeenCalledTimes", function() {
it("has a custom message on failure that tells how many times it was called", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
spy = jasmineUnderTest.createSpy('sample-spy'),
spy = new jasmineUnderTest.Env().createSpy('sample-spy'),
result;
spy();
spy();

View File

@@ -5,7 +5,7 @@ describe("toHaveBeenCalledWith", function() {
contains: jasmine.createSpy('delegated-contains').and.returnValue(true)
},
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(util),
calledSpy = jasmineUnderTest.createSpy('called-spy'),
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy'),
result;
calledSpy('a', 'b');
@@ -21,7 +21,7 @@ describe("toHaveBeenCalledWith", function() {
},
customEqualityTesters = [function() { return true; }],
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(util, customEqualityTesters),
calledSpy = jasmineUnderTest.createSpy('called-spy');
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy');
calledSpy('a', 'b');
matcher.compare(calledSpy, 'a', 'b');
@@ -34,7 +34,7 @@ describe("toHaveBeenCalledWith", function() {
contains: jasmine.createSpy('delegated-contains').and.returnValue(false)
},
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(util),
uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'),
uncalledSpy = new jasmineUnderTest.Env().createSpy('uncalled spy'),
result;
result = matcher.compare(uncalledSpy);
@@ -47,7 +47,7 @@ describe("toHaveBeenCalledWith", function() {
contains: jasmine.createSpy('delegated-contains').and.returnValue(false)
},
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(util),
calledSpy = jasmineUnderTest.createSpy('called spy'),
calledSpy = new jasmineUnderTest.Env().createSpy('called spy'),
result;
calledSpy('a');

View File

@@ -393,12 +393,17 @@ getJasmineRequireObj().Env = function(j$) {
reporter.clearReporters();
};
var spyRegistry = new j$.SpyRegistry({currentSpies: function() {
if(!currentRunnable()) {
throw new Error('Spies must be created in a before function or a spec');
var spyRegistry = new j$.SpyRegistry({
currentSpies: function() {
if(!currentRunnable()) {
throw new Error('Spies must be created in a before function or a spec');
}
return runnableResources[currentRunnable().id].spies;
},
createSpy: function(name, originalFn) {
return self.createSpy(name, originalFn);
}
return runnableResources[currentRunnable().id].spies;
}});
});
this.allowRespy = function(allow){
spyRegistry.allowRespy(allow);
@@ -412,6 +417,10 @@ getJasmineRequireObj().Env = function(j$) {
return spyRegistry.spyOnProperty.apply(spyRegistry, arguments);
};
this.createSpy = function(name, originalFn) {
return j$.Spy(name, originalFn);
};
this.createSpyObj = function(baseName, methodNames) {
var baseNameIsCollection = j$.isObject_(baseName) || j$.isArray_(baseName);
@@ -425,13 +434,13 @@ getJasmineRequireObj().Env = function(j$) {
if (j$.isArray_(methodNames)) {
for (var i = 0; i < methodNames.length; i++) {
obj[methodNames[i]] = j$.createSpy(baseName + '.' + methodNames[i]);
obj[methodNames[i]] = self.createSpy(baseName + '.' + methodNames[i]);
spiesWereSet = true;
}
} else if (j$.isObject_(methodNames)) {
for (var key in methodNames) {
if (methodNames.hasOwnProperty(key)) {
obj[key] = j$.createSpy(baseName + '.' + key);
obj[key] = self.createSpy(baseName + '.' + key);
obj[key].and.returnValue(methodNames[key]);
spiesWereSet = true;
}

View File

@@ -4,6 +4,7 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
function SpyRegistry(options) {
options = options || {};
var createSpy = options.createSpy;
var currentSpies = options.currentSpies || function() { return []; };
this.allowRespy = function(allow){
@@ -39,7 +40,7 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
}
var originalMethod = obj[methodName],
spiedMethod = j$.createSpy(methodName, originalMethod),
spiedMethod = createSpy(methodName, originalMethod),
restoreStrategy;
if (Object.prototype.hasOwnProperty.call(obj, methodName)) {
@@ -94,7 +95,7 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
}
var originalDescriptor = j$.util.clone(descriptor),
spy = j$.createSpy(propertyName, descriptor[accessType]),
spy = createSpy(propertyName, descriptor[accessType]),
restoreStrategy;
if (Object.prototype.hasOwnProperty.call(obj, propertyName)) {

View File

@@ -173,18 +173,6 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return new j$.ArrayWithExactContents(sample);
};
/**
* Create a bare {@link Spy} object. This won't be installed anywhere and will not have any implementation behind it.
* @name jasmine.createSpy
* @function
* @param {String} [name] - Name to give the spy. This will be displayed in failure messages.
* @param {Function} [originalFn] - Function to act as the real implementation.
* @return {Spy}
*/
j$.createSpy = function(name, originalFn) {
return j$.Spy(name, originalFn);
};
j$.isSpy = function(putativeSpy) {
if (!putativeSpy) {
return false;

View File

@@ -256,6 +256,18 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.clock;
};
/**
* Create a bare {@link Spy} object. This won't be installed anywhere and will not have any implementation behind it.
* @name jasmine.createSpy
* @function
* @param {String} [name] - Name to give the spy. This will be displayed in failure messages.
* @param {Function} [originalFn] - Function to act as the real implementation.
* @return {Spy}
*/
jasmine.createSpy = function(name, originalFn) {
return env.createSpy(name, originalFn);
};
/**
* Create an object with multiple {@link Spy}s as its members.
* @name jasmine.createSpyObj