Merge branch 'enelson/default' of https://github.com/elliot-nelson/jasmine
* Merges #1716 from @elliot-nelson
This commit is contained in:
@@ -1128,6 +1128,17 @@ 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) {
|
this.addSpyStrategy = function(name, fn) {
|
||||||
if (!currentRunnable()) {
|
if (!currentRunnable()) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -1206,7 +1217,8 @@ getJasmineRequireObj().Env = function(j$) {
|
|||||||
spies: [],
|
spies: [],
|
||||||
customEqualityTesters: [],
|
customEqualityTesters: [],
|
||||||
customMatchers: {},
|
customMatchers: {},
|
||||||
customSpyStrategies: {}
|
customSpyStrategies: {},
|
||||||
|
defaultStrategyFn: undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
if (runnableResources[parentRunnableId]) {
|
if (runnableResources[parentRunnableId]) {
|
||||||
@@ -1216,6 +1228,8 @@ getJasmineRequireObj().Env = function(j$) {
|
|||||||
resources.customMatchers = j$.util.clone(
|
resources.customMatchers = j$.util.clone(
|
||||||
runnableResources[parentRunnableId].customMatchers
|
runnableResources[parentRunnableId].customMatchers
|
||||||
);
|
);
|
||||||
|
resources.defaultStrategyFn =
|
||||||
|
runnableResources[parentRunnableId].defaultStrategyFn;
|
||||||
}
|
}
|
||||||
|
|
||||||
runnableResources[id] = resources;
|
runnableResources[id] = resources;
|
||||||
@@ -1653,6 +1667,15 @@ getJasmineRequireObj().Env = function(j$) {
|
|||||||
|
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
|
function getDefaultStrategyFn() {
|
||||||
|
var runnable = currentRunnable();
|
||||||
|
|
||||||
|
if (runnable) {
|
||||||
|
return runnableResources[runnable.id].defaultStrategyFn;
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
function getPromise() {
|
function getPromise() {
|
||||||
return customPromise || global.Promise;
|
return customPromise || global.Promise;
|
||||||
}
|
}
|
||||||
@@ -6760,6 +6783,22 @@ getJasmineRequireObj().interface = function(jasmine, env) {
|
|||||||
return env.addSpyStrategy(name, factory);
|
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;
|
return jasmineInterface;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -6777,7 +6816,13 @@ getJasmineRequireObj().Spy = function(j$) {
|
|||||||
* @constructor
|
* @constructor
|
||||||
* @name Spy
|
* @name Spy
|
||||||
*/
|
*/
|
||||||
function Spy(name, originalFn, customStrategies, getPromise) {
|
function Spy(
|
||||||
|
name,
|
||||||
|
originalFn,
|
||||||
|
customStrategies,
|
||||||
|
defaultStrategyFn,
|
||||||
|
getPromise
|
||||||
|
) {
|
||||||
var numArgs = typeof originalFn === 'function' ? originalFn.length : 0,
|
var numArgs = typeof originalFn === 'function' ? originalFn.length : 0,
|
||||||
wrapper = makeFunc(numArgs, function() {
|
wrapper = makeFunc(numArgs, function() {
|
||||||
return spy.apply(this, Array.prototype.slice.call(arguments));
|
return spy.apply(this, Array.prototype.slice.call(arguments));
|
||||||
@@ -6894,6 +6939,10 @@ getJasmineRequireObj().Spy = function(j$) {
|
|||||||
};
|
};
|
||||||
wrapper.calls = callTracker;
|
wrapper.calls = callTracker;
|
||||||
|
|
||||||
|
if (defaultStrategyFn) {
|
||||||
|
defaultStrategyFn(wrapper.and);
|
||||||
|
}
|
||||||
|
|
||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6967,11 +7016,17 @@ getJasmineRequireObj().Spy = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
getJasmineRequireObj().SpyFactory = function(j$) {
|
getJasmineRequireObj().SpyFactory = function(j$) {
|
||||||
function SpyFactory(getCustomStrategies, getPromise) {
|
function SpyFactory(getCustomStrategies, getDefaultStrategyFn, getPromise) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.createSpy = function(name, originalFn) {
|
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, propertyNames) {
|
this.createSpyObj = function(baseName, methodNames, propertyNames) {
|
||||||
|
|||||||
68
spec/core/integration/DefaultSpyStrategySpec.js
Normal file
68
spec/core/integration/DefaultSpyStrategySpec.js
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
describe('Default Spy Strategy (Integration)', function() {
|
||||||
|
var env;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
env = new jasmineUnderTest.Env();
|
||||||
|
env.configure({random: false});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('allows defining a default spy strategy', function(done) {
|
||||||
|
env.describe('suite with default strategy', function() {
|
||||||
|
env.beforeEach(function() {
|
||||||
|
env.setDefaultSpyStrategy(function (and) {
|
||||||
|
and.returnValue(42);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
env.it('spec in suite', function() {
|
||||||
|
var spy = env.createSpy('something');
|
||||||
|
expect(spy()).toBe(42);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
env.it('spec not in suite', function() {
|
||||||
|
var spy = env.createSpy('something');
|
||||||
|
expect(spy()).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
function jasmineDone(result) {
|
||||||
|
expect(result.overallStatus).toEqual('passed');
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
|
||||||
|
env.addReporter({ jasmineDone: jasmineDone });
|
||||||
|
env.execute();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses the default spy strategy defined when the spy is created', function (done) {
|
||||||
|
env.it('spec', function() {
|
||||||
|
var a = env.createSpy('a');
|
||||||
|
env.setDefaultSpyStrategy(function (and) { and.returnValue(42); });
|
||||||
|
var b = env.createSpy('b');
|
||||||
|
env.setDefaultSpyStrategy(function (and) { and.stub(); });
|
||||||
|
var c = env.createSpy('c');
|
||||||
|
env.setDefaultSpyStrategy();
|
||||||
|
var d = env.createSpy('d');
|
||||||
|
|
||||||
|
expect(a()).toBeUndefined();
|
||||||
|
expect(b()).toBe(42);
|
||||||
|
expect(c()).toBeUndefined();
|
||||||
|
expect(d()).toBeUndefined();
|
||||||
|
|
||||||
|
// Check our assumptions about which spies are "configured" (this matters because
|
||||||
|
// spies that use withArgs() behave differently if they are not configured).
|
||||||
|
expect(a.and.isConfigured()).toBe(false);
|
||||||
|
expect(b.and.isConfigured()).toBe(true);
|
||||||
|
expect(c.and.isConfigured()).toBe(true);
|
||||||
|
expect(d.and.isConfigured()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
function jasmineDone(result) {
|
||||||
|
expect(result.overallStatus).toEqual('passed');
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
|
||||||
|
env.addReporter({ jasmineDone: jasmineDone });
|
||||||
|
env.execute();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -222,6 +222,17 @@ 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) {
|
this.addSpyStrategy = function(name, fn) {
|
||||||
if (!currentRunnable()) {
|
if (!currentRunnable()) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -300,7 +311,8 @@ getJasmineRequireObj().Env = function(j$) {
|
|||||||
spies: [],
|
spies: [],
|
||||||
customEqualityTesters: [],
|
customEqualityTesters: [],
|
||||||
customMatchers: {},
|
customMatchers: {},
|
||||||
customSpyStrategies: {}
|
customSpyStrategies: {},
|
||||||
|
defaultStrategyFn: undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
if (runnableResources[parentRunnableId]) {
|
if (runnableResources[parentRunnableId]) {
|
||||||
@@ -310,6 +322,8 @@ getJasmineRequireObj().Env = function(j$) {
|
|||||||
resources.customMatchers = j$.util.clone(
|
resources.customMatchers = j$.util.clone(
|
||||||
runnableResources[parentRunnableId].customMatchers
|
runnableResources[parentRunnableId].customMatchers
|
||||||
);
|
);
|
||||||
|
resources.defaultStrategyFn =
|
||||||
|
runnableResources[parentRunnableId].defaultStrategyFn;
|
||||||
}
|
}
|
||||||
|
|
||||||
runnableResources[id] = resources;
|
runnableResources[id] = resources;
|
||||||
@@ -747,6 +761,15 @@ getJasmineRequireObj().Env = function(j$) {
|
|||||||
|
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
|
function getDefaultStrategyFn() {
|
||||||
|
var runnable = currentRunnable();
|
||||||
|
|
||||||
|
if (runnable) {
|
||||||
|
return runnableResources[runnable.id].defaultStrategyFn;
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
function getPromise() {
|
function getPromise() {
|
||||||
return customPromise || global.Promise;
|
return customPromise || global.Promise;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,13 @@ getJasmineRequireObj().Spy = function(j$) {
|
|||||||
* @constructor
|
* @constructor
|
||||||
* @name Spy
|
* @name Spy
|
||||||
*/
|
*/
|
||||||
function Spy(name, originalFn, customStrategies, getPromise) {
|
function Spy(
|
||||||
|
name,
|
||||||
|
originalFn,
|
||||||
|
customStrategies,
|
||||||
|
defaultStrategyFn,
|
||||||
|
getPromise
|
||||||
|
) {
|
||||||
var numArgs = typeof originalFn === 'function' ? originalFn.length : 0,
|
var numArgs = typeof originalFn === 'function' ? originalFn.length : 0,
|
||||||
wrapper = makeFunc(numArgs, function() {
|
wrapper = makeFunc(numArgs, function() {
|
||||||
return spy.apply(this, Array.prototype.slice.call(arguments));
|
return spy.apply(this, Array.prototype.slice.call(arguments));
|
||||||
@@ -129,6 +135,10 @@ getJasmineRequireObj().Spy = function(j$) {
|
|||||||
};
|
};
|
||||||
wrapper.calls = callTracker;
|
wrapper.calls = callTracker;
|
||||||
|
|
||||||
|
if (defaultStrategyFn) {
|
||||||
|
defaultStrategyFn(wrapper.and);
|
||||||
|
}
|
||||||
|
|
||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
getJasmineRequireObj().SpyFactory = function(j$) {
|
getJasmineRequireObj().SpyFactory = function(j$) {
|
||||||
function SpyFactory(getCustomStrategies, getPromise) {
|
function SpyFactory(getCustomStrategies, getDefaultStrategyFn, getPromise) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.createSpy = function(name, originalFn) {
|
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, propertyNames) {
|
this.createSpyObj = function(baseName, methodNames, propertyNames) {
|
||||||
|
|||||||
@@ -354,5 +354,21 @@ getJasmineRequireObj().interface = function(jasmine, env) {
|
|||||||
return env.addSpyStrategy(name, factory);
|
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;
|
return jasmineInterface;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user