From 1555a4911ad5360c145c0ddc8ec6c4bf9a381c13 Mon Sep 17 00:00:00 2001 From: Marcin Wosinek Date: Sun, 6 Aug 2017 19:55:30 +0100 Subject: [PATCH] fix(ngMock): pass unexpected request failures in `$httpBackend` to the error handler Closes #16150 Closes #15855 --- src/ng/q.js | 4 ++++ src/ngMock/angular-mocks.js | 8 +++++++- test/ngMock/angular-mocksSpec.js | 21 ++++++++++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/ng/q.js b/src/ng/q.js index 350923378f80..136e2335c914 100644 --- a/src/ng/q.js +++ b/src/ng/q.js @@ -364,6 +364,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.$$passToExceptionHandler === true) { + exceptionHandler(e); + } } } } finally { diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index e9b4131cb06b..a5a24bd18935 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1438,10 +1438,16 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) { return; } } - throw wasExpected ? + var error = wasExpected ? new Error('No response defined !') : new Error('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). + error.$$passToExceptionHandler = true; + + throw error; } /** diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 32ddee0a1d39..30791fdc54bd 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -2512,7 +2512,7 @@ describe('ngMock', function() { describe('ngMockE2E', function() { describe('$httpBackend', function() { - var hb, realHttpBackend, realHttpBackendBrowser, callback; + var hb, realHttpBackend, $http, realHttpBackendBrowser, callback; beforeEach(function() { callback = jasmine.createSpy('callback'); @@ -2525,10 +2525,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};