Improve error message when passing a non-function to callFake

This commit is contained in:
Andrzej Kopeć
2016-02-28 21:36:09 +01:00
parent b6798cdb06
commit bbf3f6825c
2 changed files with 17 additions and 2 deletions

View File

@@ -92,6 +92,18 @@ describe("SpyStrategy", function() {
expect(returnValue).toEqual(67);
});
it('throws an error when a non-function is passed to callFake strategy', function() {
var originalFn = jasmine.createSpy('original'),
invalidFakes = [5, 'foo', {}, true, false, null, void 0, new Date(), /.*/];
for (var i=0; i<invalidFakes.length; i++) {
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);
}
});
it("allows a return to plan stubbing after another strategy", function() {
var originalFn = jasmine.createSpy("original"),
fakeFn = jasmine.createSpy("fake").and.returnValue(67),
@@ -103,7 +115,7 @@ describe("SpyStrategy", function() {
expect(originalFn).not.toHaveBeenCalled();
expect(returnValue).toEqual(67);
spyStrategy.stub();
returnValue = spyStrategy.exec();
@@ -118,7 +130,7 @@ describe("SpyStrategy", function() {
expect(spyStrategy.callThrough()).toBe(spy);
expect(spyStrategy.returnValue()).toBe(spy);
expect(spyStrategy.throwError()).toBe(spy);
expect(spyStrategy.callFake()).toBe(spy);
expect(spyStrategy.callFake(function() {})).toBe(spy);
expect(spyStrategy.stub()).toBe(spy);
});
});

View File

@@ -45,6 +45,9 @@ getJasmineRequireObj().SpyStrategy = function() {
};
this.callFake = function(fn) {
if(!(fn instanceof Function)) {
throw new Error('Argument passed to callFake should be a function, got ' + fn);
}
plan = fn;
return getSpy();
};