Allow users to set a default spy strategy
This commit is contained in:
@@ -208,6 +208,13 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
}
|
||||
});
|
||||
|
||||
this.setDefaultSpyStrategy = function(defaultStrategyFn) {
|
||||
if (!currentRunnable()) {
|
||||
throw new Error('Default spy strategy must be set in a before function or a spec');
|
||||
}
|
||||
runnableResources[currentRunnable().id].defaultStrategyFn = defaultStrategyFn;
|
||||
};
|
||||
|
||||
this.addSpyStrategy = function(name, fn) {
|
||||
if (!currentRunnable()) {
|
||||
throw new Error(
|
||||
@@ -286,7 +293,8 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
spies: [],
|
||||
customEqualityTesters: [],
|
||||
customMatchers: {},
|
||||
customSpyStrategies: {}
|
||||
customSpyStrategies: {},
|
||||
defaultStrategyFn: undefined
|
||||
};
|
||||
|
||||
if (runnableResources[parentRunnableId]) {
|
||||
@@ -296,6 +304,7 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
resources.customMatchers = j$.util.clone(
|
||||
runnableResources[parentRunnableId].customMatchers
|
||||
);
|
||||
resources.defaultStrategyFn = runnableResources[parentRunnableId].defaultStrategyFn;
|
||||
}
|
||||
|
||||
runnableResources[id] = resources;
|
||||
@@ -720,6 +729,15 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
|
||||
return {};
|
||||
},
|
||||
function getDefaultStrategyFn() {
|
||||
var runnable = currentRunnable();
|
||||
|
||||
if (runnable) {
|
||||
return runnableResources[runnable.id].defaultStrategyFn;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
function getPromise() {
|
||||
return customPromise || global.Promise;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ getJasmineRequireObj().Spy = function(j$) {
|
||||
* @constructor
|
||||
* @name Spy
|
||||
*/
|
||||
function Spy(name, originalFn, customStrategies, getPromise) {
|
||||
function Spy(name, originalFn, customStrategies, defaultStrategyFn, getPromise) {
|
||||
var numArgs = typeof originalFn === 'function' ? originalFn.length : 0,
|
||||
wrapper = makeFunc(numArgs, function() {
|
||||
return spy.apply(this, Array.prototype.slice.call(arguments));
|
||||
@@ -127,6 +127,10 @@ getJasmineRequireObj().Spy = function(j$) {
|
||||
};
|
||||
wrapper.calls = callTracker;
|
||||
|
||||
if (defaultStrategyFn) {
|
||||
defaultStrategyFn(wrapper.and);
|
||||
}
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
getJasmineRequireObj().SpyFactory = function(j$) {
|
||||
function SpyFactory(getCustomStrategies, getPromise) {
|
||||
function SpyFactory(getCustomStrategies, getDefaultStrategyFn, getPromise) {
|
||||
var self = this;
|
||||
|
||||
this.createSpy = function(name, originalFn) {
|
||||
return j$.Spy(name, originalFn, getCustomStrategies(), getPromise);
|
||||
return j$.Spy(name, originalFn, getCustomStrategies(), getDefaultStrategyFn(), getPromise);
|
||||
};
|
||||
|
||||
this.createSpyObj = function(baseName, methodNames) {
|
||||
|
||||
@@ -330,5 +330,21 @@ getJasmineRequireObj().interface = function(jasmine, env) {
|
||||
return env.addSpyStrategy(name, factory);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the default spy strategy for the current scope of specs.
|
||||
*
|
||||
* _Note:_ This is only callable from within a {@link beforeEach}, {@link it}, or {@link beforeAll}.
|
||||
* @name jasmine.setDefaultSpyStrategy
|
||||
* @function
|
||||
* @param {Function} defaultStrategyFn - a function that assigns a strategy
|
||||
* @example
|
||||
* beforeEach(function() {
|
||||
* jasmine.setDefaultSpyStrategy(and => and.returnValue(true));
|
||||
* });
|
||||
*/
|
||||
jasmine.setDefaultSpyStrategy = function(defaultStrategyFn) {
|
||||
return env.setDefaultSpyStrategy(defaultStrategyFn);
|
||||
};
|
||||
|
||||
return jasmineInterface;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user