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

Commit

Permalink
fix(ngMock): pass unexpected request failures in $httpBackend to th…
Browse files Browse the repository at this point in the history
…e error handler

Closes #16150
Closes #15855
  • Loading branch information
marcin-wosinek authored and petebacondarwin committed Sep 18, 2017
1 parent dbba98b commit 1555a49
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
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 @@ -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');
Expand All @@ -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};
Expand Down

0 comments on commit 1555a49

Please sign in to comment.