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

ugly solution for unexpected request fail on verify no outstanding request #16150

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
}
});

function PassToExceptionHandlerError(e) {
this.message = e.message;
this.name = e.name;
if (e.line) this.line = e.line;
if (e.sourceId) this.sourceId = e.sourceId;
if (e.stack) this.stack = e.stack;
if (e.stackArray) this.stackArray = e.stackArray;
}

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

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

Expand All @@ -364,6 +375,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 +683,7 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
$Q.resolve = resolve;
$Q.all = all;
$Q.race = race;
$Q.$$PassToExceptionHandlerError = PassToExceptionHandlerError;

return $Q;
}
Expand Down
20 changes: 16 additions & 4 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,22 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
return;
}
}
throw wasExpected ?

// 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).
var error = wasExpected ?
new Error('No response defined !') :
new Error('Unexpected request: ' + method + ' ' + url + '\n' +
(expectation ? 'Expected ' + expectation : 'No more request expected'));

// IE10+ and PhanthomJS do not set stack trace information, until the error is thrown
if (!error.stack) {
try {
throw error;
} catch (e) { /* empty */ }
}

throw new $q.$$PassToExceptionHandlerError(error);
}

/**
Expand Down Expand Up @@ -2706,7 +2718,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
21 changes: 20 additions & 1 deletion test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,7 @@ describe('ngMock', function() {

describe('ngMockE2E', function() {
describe('$httpBackend', function() {
var hb, realHttpBackend, realHttpBackendBrowser, callback;
var hb, realHttpBackend, realHttpBackendBrowser, $http, callback;

beforeEach(function() {
callback = jasmine.createSpy('callback');
Expand All @@ -2442,10 +2442,29 @@ describe('ngMockE2E', function() {
module('ngMockE2E');
inject(function($injector) {
hb = $injector.get('$httpBackend');
$http = $injector.get('$http');
});
});


it('should throw error when unexpected request - without error callback', function() {
expect(function() {
$http.get('/some').then(noop);

hb.verifyNoOutstandingRequest();
}).toThrowError('Unexpected request: GET /some\nNo more request expected');
});


it('should throw error when unexpected request - with error callback', function() {
expect(function() {
$http.get('/some').then(noop, noop);

hb.verifyNoOutstandingRequest();
}).toThrowError('Unexpected request: GET /some\nNo more request expected');
});


describe('passThrough()', function() {
it('should delegate requests to the real backend when passThrough is invoked', function() {
var eventHandlers = {progress: angular.noop};
Expand Down