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

Commit

Permalink
fix($httpBackend): solution for unexpected request fail on verify no …
Browse files Browse the repository at this point in the history
…outstanding request
  • Loading branch information
marcin-wosinek committed Sep 6, 2017
1 parent 3bbe17a commit 8f480de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
}
});

function PassToExceptionHandlerError(message) {
this.name = '$$PassToExceptionHandlerError';
this.message = message;
this.stack = (new Error()).stack;
}

PassToExceptionHandlerError.prototype = Object.create(Error.prototype);

function processQueue(state) {
var fn, promise, pending;

Expand All @@ -364,6 +372,10 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
}
} catch (e) {
rejectPromise(promise, e);
// This error is explicitly marked for being passed to the $exceptionHandler
if (e && e instanceof PassToExceptionHandlerError) {
exceptionHandler(e);
}
}
}
} finally {
Expand Down Expand Up @@ -668,6 +680,7 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
$Q.resolve = resolve;
$Q.all = all;
$Q.race = race;
$Q.$$PassToExceptionHandlerError = PassToExceptionHandlerError;

return $Q;
}
Expand Down
17 changes: 11 additions & 6 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ angular.mock.dump = function(object) {
```
*/
angular.mock.$httpBackendDecorator =
['$rootScope', '$timeout', '$delegate', createHttpBackendMock];
['$q', '$rootScope', '$timeout', '$delegate', createHttpBackendMock];

/**
* General factory function for $httpBackend mock.
Expand All @@ -1339,7 +1339,7 @@ angular.mock.$httpBackendDecorator =
* @param {Object=} $browser Auto-flushing enabled if specified
* @return {Object} Instance of $httpBackend mock
*/
function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
function createHttpBackendMock($q, $rootScope, $timeout, $delegate, $browser) {
var definitions = [],
expectations = [],
responses = [],
Expand Down Expand Up @@ -1438,10 +1438,15 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
return;
}
}
throw wasExpected ?
new Error('No response defined !') :
new Error('Unexpected request: ' + method + ' ' + url + '\n' +
var error = wasExpected ?
new $q.$$PassToExceptionHandlerError('No response defined !') :
new $q.$$PassToExceptionHandlerError('Unexpected request: ' + method + ' ' + url + '\n' +
(expectation ? 'Expected ' + expectation : 'No more request expected'));

// In addition to be being converted to a rejection, this error also needs to be passed to
// the $exceptionHandler and be rethrown (so that the test fails).

throw error;
}

/**
Expand Down Expand Up @@ -2706,7 +2711,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
*/
angular.mock.e2e = {};
angular.mock.e2e.$httpBackendDecorator =
['$rootScope', '$timeout', '$delegate', '$browser', createHttpBackendMock];
['$q', '$rootScope', '$timeout', '$delegate', '$browser', createHttpBackendMock];


/**
Expand Down

0 comments on commit 8f480de

Please sign in to comment.