Moved createSpyObj to env so it can be stateful

This commit is contained in:
Steve Gravrock
2018-01-09 09:53:45 -08:00
parent 16e07a0e99
commit 298b5ba127
5 changed files with 102 additions and 88 deletions

View File

@@ -412,6 +412,39 @@ getJasmineRequireObj().Env = function(j$) {
return spyRegistry.spyOnProperty.apply(spyRegistry, arguments);
};
this.createSpyObj = function(baseName, methodNames) {
var baseNameIsCollection = j$.isObject_(baseName) || j$.isArray_(baseName);
if (baseNameIsCollection && j$.util.isUndefined(methodNames)) {
methodNames = baseName;
baseName = 'unknown';
}
var obj = {};
var spiesWereSet = false;
if (j$.isArray_(methodNames)) {
for (var i = 0; i < methodNames.length; i++) {
obj[methodNames[i]] = j$.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].and.returnValue(methodNames[key]);
spiesWereSet = true;
}
}
}
if (!spiesWereSet) {
throw 'createSpyObj requires a non-empty array or object of method names to create spies for';
}
return obj;
};
var ensureIsFunction = function(fn, caller) {
if (!j$.isFunction_(fn)) {
throw new Error(caller + ' expects a function argument; received ' + j$.getType_(fn));