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

Commit f18dd29

Browse files
marcin-wosinekpetebacondarwin
authored andcommitted
fix(ngMock): pass unexpected request failures in $httpBackend to the error handler
Closes #16150 Closes #15855
1 parent 97b00ca commit f18dd29

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/ng/q.js

+4
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
364364
}
365365
} catch (e) {
366366
rejectPromise(promise, e);
367+
// This error is explicitly marked for being passed to the $exceptionHandler
368+
if (e && e.$$passToExceptionHandler === true) {
369+
exceptionHandler(e);
370+
}
367371
}
368372
}
369373
} finally {

src/ngMock/angular-mocks.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1438,10 +1438,16 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
14381438
return;
14391439
}
14401440
}
1441-
throw wasExpected ?
1441+
var error = wasExpected ?
14421442
new Error('No response defined !') :
14431443
new Error('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+
error.$$passToExceptionHandler = true;
1449+
1450+
throw error;
14451451
}
14461452

14471453
/**

test/ngMock/angular-mocksSpec.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -2429,7 +2429,7 @@ describe('ngMock', function() {
24292429

24302430
describe('ngMockE2E', function() {
24312431
describe('$httpBackend', function() {
2432-
var hb, realHttpBackend, realHttpBackendBrowser, callback;
2432+
var hb, realHttpBackend, realHttpBackendBrowser, $http, callback;
24332433

24342434
beforeEach(function() {
24352435
callback = jasmine.createSpy('callback');
@@ -2442,10 +2442,29 @@ describe('ngMockE2E', function() {
24422442
module('ngMockE2E');
24432443
inject(function($injector) {
24442444
hb = $injector.get('$httpBackend');
2445+
$http = $injector.get('$http');
24452446
});
24462447
});
24472448

24482449

2450+
it('should throw error when unexpected request - without error callback', function() {
2451+
expect(function() {
2452+
$http.get('/some').then(noop);
2453+
2454+
hb.verifyNoOutstandingRequest();
2455+
}).toThrowError('Unexpected request: GET /some\nNo more request expected');
2456+
});
2457+
2458+
2459+
it('should throw error when unexpected request - with error callback', function() {
2460+
expect(function() {
2461+
$http.get('/some').then(noop, noop);
2462+
2463+
hb.verifyNoOutstandingRequest();
2464+
}).toThrowError('Unexpected request: GET /some\nNo more request expected');
2465+
});
2466+
2467+
24492468
describe('passThrough()', function() {
24502469
it('should delegate requests to the real backend when passThrough is invoked', function() {
24512470
var eventHandlers = {progress: angular.noop};

0 commit comments

Comments
 (0)