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

@@ -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