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

Commit d548e10

Browse files
committed
fix(ngMock): pass failed HTTP expectations to $errorHandler
This was only partially fixed in f18dd29
1 parent e500fb6 commit d548e10

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/ngMock/angular-mocks.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -1511,15 +1511,25 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
15111511
}
15121512

15131513
if (expectation && expectation.match(method, url)) {
1514+
var expectationError;
1515+
15141516
if (!expectation.matchData(data)) {
1515-
throw new Error('Expected ' + expectation + ' with different data\n' +
1516-
'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
1517+
expectationError = new Error('Expected ' + expectation + ' with different data\n' +
1518+
'EXPECTED: ' + prettyPrint(expectation.data) + '\n' +
1519+
'GOT: ' + data);
15171520
}
15181521

15191522
if (!expectation.matchHeaders(headers)) {
1520-
throw new Error('Expected ' + expectation + ' with different headers\n' +
1521-
'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' +
1522-
prettyPrint(headers));
1523+
expectationError = new Error('Expected ' + expectation + ' with different headers\n' +
1524+
'EXPECTED: ' + prettyPrint(expectation.headers) + '\n' +
1525+
'GOT: ' + prettyPrint(headers));
1526+
}
1527+
1528+
if (expectationError) {
1529+
// In addition to be being converted to a rejection, this error also needs to be passed to
1530+
// the $exceptionHandler and be rethrown (so that the test fails).
1531+
expectationError.$$passToExceptionHandler = true;
1532+
throw expectationError;
15231533
}
15241534

15251535
expectations.shift();

test/ngMock/angular-mocksSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,24 @@ describe('ngMock', function() {
15061506
});
15071507

15081508

1509+
it('should throw error when expectation fails', function() {
1510+
expect(function() {
1511+
hb.expectPOST('/some', { foo: 1 }).respond({});
1512+
hb('POST', '/some', { foo: 2 }, callback);
1513+
hb.flush();
1514+
}).toThrowError(/^Expected POST \/some with different data/);
1515+
});
1516+
1517+
1518+
it('should throw error when expectation about headers fails', function() {
1519+
expect(function() {
1520+
hb.expectPOST('/some', { foo: 1 }, {X: 'val1'}).respond({});
1521+
hb('POST', '/some', { foo: 1 }, callback, {X: 'val2'});
1522+
hb.flush();
1523+
}).toThrowError(/^Expected POST \/some with different headers/);
1524+
});
1525+
1526+
15091527
it('should match headers if specified', function() {
15101528
hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1');
15111529
hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2');
@@ -2833,6 +2851,24 @@ describe('ngMockE2E', function() {
28332851
}).toThrowError('Unexpected request: GET /some\nNo more request expected');
28342852
});
28352853

2854+
it('should throw error when expectation fails - without error callback', function() {
2855+
expect(function() {
2856+
hb.expectPOST('/some', { foo: 1 }).respond({});
2857+
$http.post('/some', { foo: 2 }).then(noop);
2858+
2859+
hb.flush();
2860+
}).toThrowError(/^Expected POST \/some with different data/);
2861+
});
2862+
2863+
it('should throw error when unexpected request - with error callback', function() {
2864+
expect(function() {
2865+
hb.expectPOST('/some', { foo: 1 }).respond({});
2866+
$http.post('/some', { foo: 2 }).then(noop, noop);
2867+
2868+
hb.flush();
2869+
}).toThrowError(/^Expected POST \/some with different data/);
2870+
});
2871+
28362872

28372873
describe('passThrough()', function() {
28382874
it('should delegate requests to the real backend when passThrough is invoked', function() {

0 commit comments

Comments
 (0)