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

Commit 490d1f7

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

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-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

+45
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,33 @@ 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+
1527+
it('should throw error when expectation about both data and headers fails', function() {
1528+
expect(function() {
1529+
hb.expectPOST('/some', {foo: 1}, {X: 'val1'}).respond({});
1530+
hb('POST', '/some', {foo: 2}, callback, {X: 'val2'});
1531+
hb.flush();
1532+
}).toThrowError(/^Expected POST \/some with different/);
1533+
});
1534+
1535+
15091536
it('should match headers if specified', function() {
15101537
hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1');
15111538
hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2');
@@ -2833,6 +2860,24 @@ describe('ngMockE2E', function() {
28332860
}).toThrowError('Unexpected request: GET /some\nNo more request expected');
28342861
});
28352862

2863+
it('should throw error when expectation fails - without error callback', function() {
2864+
expect(function() {
2865+
hb.expectPOST('/some', { foo: 1 }).respond({});
2866+
$http.post('/some', { foo: 2 }).then(noop);
2867+
2868+
hb.flush();
2869+
}).toThrowError(/^Expected POST \/some with different data/);
2870+
});
2871+
2872+
it('should throw error when unexpected request - with error callback', function() {
2873+
expect(function() {
2874+
hb.expectPOST('/some', { foo: 1 }).respond({});
2875+
$http.post('/some', { foo: 2 }).then(noop, noop);
2876+
2877+
hb.flush();
2878+
}).toThrowError(/^Expected POST \/some with different data/);
2879+
});
2880+
28362881

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

0 commit comments

Comments
 (0)