From acb066e84a10483e1025eed295352b66747dbb8a Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Sun, 23 Nov 2014 14:27:19 +0100 Subject: [PATCH] fix(ngMock): allow numeric timeouts in $httpBackend mock Fixes #4891 --- src/ngMock/angular-mocks.js | 10 ++++++---- test/ngMock/angular-mocksSpec.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 8545b6b8..6f3fd51a 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1112,7 +1112,7 @@ angular.mock.dump = function(object) { ``` */ angular.mock.$HttpBackendProvider = function() { - this.$get = ['$rootScope', createHttpBackendMock]; + this.$get = ['$rootScope', '$timeout', createHttpBackendMock]; }; /** @@ -1129,7 +1129,7 @@ angular.mock.$HttpBackendProvider = function() { * @param {Object=} $browser Auto-flushing enabled if specified * @return {Object} Instance of $httpBackend mock */ -function createHttpBackendMock($rootScope, $delegate, $browser) { +function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) { var definitions = [], expectations = [], responses = [], @@ -1159,7 +1159,9 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { } function wrapResponse(wrapped) { - if (!$browser && timeout && timeout.then) timeout.then(handleTimeout); + if (!$browser && timeout) { + timeout.then ? timeout.then(handleTimeout) : $timeout(handleTimeout, timeout); + } return handleResponse; @@ -2033,7 +2035,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) { */ angular.mock.e2e = {}; angular.mock.e2e.$httpBackendDecorator = - ['$rootScope', '$delegate', '$browser', createHttpBackendMock]; + ['$rootScope', '$timeout', '$delegate', '$browser', createHttpBackendMock]; /** diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index c22c95f4..d9773b42 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1310,6 +1310,18 @@ describe('ngMock', function() { }); + it('should abort requests when timeout passed as a numeric value', inject(function($timeout) { + hb.expect('GET', '/url1').respond(200); + + hb('GET', '/url1', null, callback, null, 200); + $timeout.flush(300); + + expect(callback).toHaveBeenCalledWith(-1, undefined, ''); + hb.verifyNoOutstandingExpectation(); + hb.verifyNoOutstandingRequest(); + })); + + it('should throw an exception if no response defined', function() { hb.when('GET', '/test'); expect(function() {