Allowed async functions to be passed into spy#callFake
This commit is contained in:
@@ -5010,7 +5010,7 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
|||||||
* @param {Function} fn The function to invoke with the passed parameters.
|
* @param {Function} fn The function to invoke with the passed parameters.
|
||||||
*/
|
*/
|
||||||
this.callFake = function(fn) {
|
this.callFake = function(fn) {
|
||||||
if(!j$.isFunction_(fn)) {
|
if(!(j$.isFunction_(fn) || j$.isAsyncFunction_(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;
|
||||||
|
|||||||
@@ -92,16 +92,40 @@ describe("SpyStrategy", function() {
|
|||||||
expect(returnValue).toEqual(67);
|
expect(returnValue).toEqual(67);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("allows a fake async function to be called instead", async function(done) {
|
||||||
|
try {
|
||||||
|
var originalFn = jasmine.createSpy("original"),
|
||||||
|
fakeFn = jasmine.createSpy("fake").and.callFake(async function () { return 67; }),
|
||||||
|
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
|
||||||
|
returnValue;
|
||||||
|
|
||||||
|
spyStrategy.callFake(fakeFn);
|
||||||
|
returnValue = await spyStrategy.exec();
|
||||||
|
|
||||||
|
expect(originalFn).not.toHaveBeenCalled();
|
||||||
|
expect(returnValue).toEqual(67);
|
||||||
|
|
||||||
|
done();
|
||||||
|
} catch (err) {
|
||||||
|
done.fail(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
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}),
|
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(), /.*/];
|
||||||
|
|
||||||
spyOn(jasmineUnderTest, 'isFunction_').and.returnValue(false);
|
spyOn(jasmineUnderTest, 'isFunction_').and.returnValue(false);
|
||||||
|
spyOn(jasmineUnderTest, 'isAsyncFunction_').and.returnValue(false);
|
||||||
|
|
||||||
expect(function () {
|
expect(function () {
|
||||||
spyStrategy.callFake(function() {});
|
spyStrategy.callFake(function() {});
|
||||||
}).toThrowError(/^Argument passed to callFake should be a function, got/);
|
}).toThrowError(/^Argument passed to callFake should be a function, got/);
|
||||||
|
|
||||||
|
expect(function () {
|
||||||
|
spyStrategy.callFake(async 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() {
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
|||||||
* @param {Function} fn The function to invoke with the passed parameters.
|
* @param {Function} fn The function to invoke with the passed parameters.
|
||||||
*/
|
*/
|
||||||
this.callFake = function(fn) {
|
this.callFake = function(fn) {
|
||||||
if(!j$.isFunction_(fn)) {
|
if(!(j$.isFunction_(fn) || j$.isAsyncFunction_(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;
|
||||||
|
|||||||
Reference in New Issue
Block a user