Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 51a9af9

Browse files
fix($httpBackend): solution for unexpected request fail on verify no outstanding request
1 parent 3bbe17a commit 51a9af9

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/ng/q.js

+13
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
343343
}
344344
});
345345

346+
function PassToExceptionHandlerError(message) {
347+
this.name = '$$PassToExceptionHandlerError';
348+
this.message = message;
349+
this.stack = (new Error()).stack;
350+
}
351+
352+
PassToExceptionHandlerError.prototype = Object.create(Error.prototype);
353+
346354
function processQueue(state) {
347355
var fn, promise, pending;
348356

@@ -364,6 +372,10 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
364372
}
365373
} catch (e) {
366374
rejectPromise(promise, e);
375+
// This error is explicitly marked for being passed to the $exceptionHandler
376+
if (e && e instanceof PassToExceptionHandlerError) {
377+
exceptionHandler(e);
378+
}
367379
}
368380
}
369381
} finally {
@@ -668,6 +680,7 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
668680
$Q.resolve = resolve;
669681
$Q.all = all;
670682
$Q.race = race;
683+
$Q.$$PassToExceptionHandlerError = PassToExceptionHandlerError;
671684

672685
return $Q;
673686
}

src/ngMock/angular-mocks.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ angular.mock.dump = function(object) {
13231323
```
13241324
*/
13251325
angular.mock.$httpBackendDecorator =
1326-
['$rootScope', '$timeout', '$delegate', createHttpBackendMock];
1326+
['$q', '$rootScope', '$timeout', '$delegate', createHttpBackendMock];
13271327

13281328
/**
13291329
* General factory function for $httpBackend mock.
@@ -1339,7 +1339,7 @@ angular.mock.$httpBackendDecorator =
13391339
* @param {Object=} $browser Auto-flushing enabled if specified
13401340
* @return {Object} Instance of $httpBackend mock
13411341
*/
1342-
function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1342+
function createHttpBackendMock($q, $rootScope, $timeout, $delegate, $browser) {
13431343
var definitions = [],
13441344
expectations = [],
13451345
responses = [],
@@ -1438,10 +1438,15 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
14381438
return;
14391439
}
14401440
}
1441-
throw wasExpected ?
1442-
new Error('No response defined !') :
1443-
new Error('Unexpected request: ' + method + ' ' + url + '\n' +
1441+
var error = wasExpected ?
1442+
new $q.$$PassToExceptionHandlerError('No response defined !') :
1443+
new $q.$$PassToExceptionHandlerError('Unexpected request: ' + method + ' ' + url + '\n' +
14441444
(expectation ? 'Expected ' + expectation : 'No more request expected'));
1445+
1446+
// In addition to be being converted to a rejection, this error also needs to be passed to
1447+
// the $exceptionHandler and be rethrown (so that the test fails).
1448+
1449+
throw error;
14451450
}
14461451

14471452
/**
@@ -2706,7 +2711,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
27062711
*/
27072712
angular.mock.e2e = {};
27082713
angular.mock.e2e.$httpBackendDecorator =
2709-
['$rootScope', '$timeout', '$delegate', '$browser', createHttpBackendMock];
2714+
['$q', '$rootScope', '$timeout', '$delegate', '$browser', createHttpBackendMock];
27102715

27112716

27122717
/**

0 commit comments

Comments
 (0)