Skip to content

Commit 1555a49

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

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/ng/q.js

Lines changed: 4 additions & 0 deletions
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2512,7 +2512,7 @@ describe('ngMock', function() {
25122512

25132513
describe('ngMockE2E', function() {
25142514
describe('$httpBackend', function() {
2515-
var hb, realHttpBackend, realHttpBackendBrowser, callback;
2515+
var hb, realHttpBackend, $http, realHttpBackendBrowser, callback;
25162516

25172517
beforeEach(function() {
25182518
callback = jasmine.createSpy('callback');
@@ -2525,10 +2525,29 @@ describe('ngMockE2E', function() {
25252525
module('ngMockE2E');
25262526
inject(function($injector) {
25272527
hb = $injector.get('$httpBackend');
2528+
$http = $injector.get('$http');
25282529
});
25292530
});
25302531

25312532

2533+
it('should throw error when unexpected request - without error callback', function() {
2534+
expect(function() {
2535+
$http.get('/some').then(noop);
2536+
2537+
hb.verifyNoOutstandingRequest();
2538+
}).toThrowError('Unexpected request: GET /some\nNo more request expected');
2539+
});
2540+
2541+
2542+
it('should throw error when unexpected request - with error callback', function() {
2543+
expect(function() {
2544+
$http.get('/some').then(noop, noop);
2545+
2546+
hb.verifyNoOutstandingRequest();
2547+
}).toThrowError('Unexpected request: GET /some\nNo more request expected');
2548+
});
2549+
2550+
25322551
describe('passThrough()', function() {
25332552
it('should delegate requests to the real backend when passThrough is invoked', function() {
25342553
var eventHandlers = {progress: angular.noop};

0 commit comments

Comments
 (0)