Use isFunction to check for functionness in callFake

- Fixes #1191
This commit is contained in:
Gregg Van Hove
2016-09-14 16:06:44 -07:00
parent 8624a52ee0
commit 4e47b78f1f
5 changed files with 19 additions and 11 deletions

View File

@@ -67,7 +67,7 @@ var getJasmineRequireObj = (function (jasmineGlobal) {
j$.ReportDispatcher = jRequire.ReportDispatcher(); j$.ReportDispatcher = jRequire.ReportDispatcher();
j$.Spec = jRequire.Spec(j$); j$.Spec = jRequire.Spec(j$);
j$.SpyRegistry = jRequire.SpyRegistry(j$); j$.SpyRegistry = jRequire.SpyRegistry(j$);
j$.SpyStrategy = jRequire.SpyStrategy(); j$.SpyStrategy = jRequire.SpyStrategy(j$);
j$.StringMatching = jRequire.StringMatching(j$); j$.StringMatching = jRequire.StringMatching(j$);
j$.Suite = jRequire.Suite(j$); j$.Suite = jRequire.Suite(j$);
j$.Timer = jRequire.Timer(); j$.Timer = jRequire.Timer();
@@ -147,6 +147,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return j$.isA_('Number', value); return j$.isA_('Number', value);
}; };
j$.isFunction_ = function(value) {
return j$.isA_('Function', value);
};
j$.isA_ = function(typeName, value) { j$.isA_ = function(typeName, value) {
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
}; };
@@ -2120,7 +2124,7 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
return SpyRegistry; return SpyRegistry;
}; };
getJasmineRequireObj().SpyStrategy = function() { getJasmineRequireObj().SpyStrategy = function(j$) {
function SpyStrategy(options) { function SpyStrategy(options) {
options = options || {}; options = options || {};
@@ -2167,7 +2171,7 @@ getJasmineRequireObj().SpyStrategy = function() {
}; };
this.callFake = function(fn) { this.callFake = function(fn) {
if(!(fn instanceof Function)) { if(!j$.isFunction_(fn)) {
throw new Error('Argument passed to callFake should be a function, got ' + fn); throw new Error('Argument passed to callFake should be a function, got ' + fn);
} }
plan = fn; plan = fn;

View File

@@ -94,14 +94,14 @@ describe("SpyStrategy", function() {
it('throws an error when a non-function is passed to callFake strategy', function() { it('throws an error when a non-function is passed to callFake strategy', function() {
var originalFn = jasmine.createSpy('original'), var originalFn = jasmine.createSpy('original'),
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
invalidFakes = [5, 'foo', {}, true, false, null, void 0, new Date(), /.*/]; invalidFakes = [5, 'foo', {}, true, false, null, void 0, new Date(), /.*/];
for (var i=0; i<invalidFakes.length; i++) { spyOn(jasmineUnderTest, 'isFunction_').and.returnValue(false);
var invalidFake = invalidFakes[i],
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn});
expect(function() {spyStrategy.callFake(invalidFake);}).toThrowError('Argument passed to callFake should be a function, got ' + invalidFake); expect(function () {
} spyStrategy.callFake(function() {});
}).toThrowError(/^Argument passed to callFake should be a function, got/);
}); });
it("allows a return to plan stubbing after another strategy", function() { it("allows a return to plan stubbing after another strategy", function() {

View File

@@ -1,4 +1,4 @@
getJasmineRequireObj().SpyStrategy = function() { getJasmineRequireObj().SpyStrategy = function(j$) {
function SpyStrategy(options) { function SpyStrategy(options) {
options = options || {}; options = options || {};
@@ -45,7 +45,7 @@ getJasmineRequireObj().SpyStrategy = function() {
}; };
this.callFake = function(fn) { this.callFake = function(fn) {
if(!(fn instanceof Function)) { if(!j$.isFunction_(fn)) {
throw new Error('Argument passed to callFake should be a function, got ' + fn); throw new Error('Argument passed to callFake should be a function, got ' + fn);
} }
plan = fn; plan = fn;

View File

@@ -29,6 +29,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return j$.isA_('Number', value); return j$.isA_('Number', value);
}; };
j$.isFunction_ = function(value) {
return j$.isA_('Function', value);
};
j$.isA_ = function(typeName, value) { j$.isA_ = function(typeName, value) {
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
}; };

View File

@@ -45,7 +45,7 @@ var getJasmineRequireObj = (function (jasmineGlobal) {
j$.ReportDispatcher = jRequire.ReportDispatcher(); j$.ReportDispatcher = jRequire.ReportDispatcher();
j$.Spec = jRequire.Spec(j$); j$.Spec = jRequire.Spec(j$);
j$.SpyRegistry = jRequire.SpyRegistry(j$); j$.SpyRegistry = jRequire.SpyRegistry(j$);
j$.SpyStrategy = jRequire.SpyStrategy(); j$.SpyStrategy = jRequire.SpyStrategy(j$);
j$.StringMatching = jRequire.StringMatching(j$); j$.StringMatching = jRequire.StringMatching(j$);
j$.Suite = jRequire.Suite(j$); j$.Suite = jRequire.Suite(j$);
j$.Timer = jRequire.Timer(); j$.Timer = jRequire.Timer();