Skip to content
Permalink
Browse files

fix(ngMock): allow numeric timeouts in $httpBackend mock

Fixes #4891
  • Loading branch information
pkozlowski-opensource authored and lgalfaso committed Nov 23, 2014
1 parent ed1243f commit acb066e84a10483e1025eed295352b66747dbb8a
Showing with 18 additions and 4 deletions.
  1. +6 −4 src/ngMock/angular-mocks.js
  2. +12 −0 test/ngMock/angular-mocksSpec.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];


/**
@@ -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() {

0 comments on commit acb066e

Please sign in to comment.
You can’t perform that action at this time.