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

Commit ebeb1c9

Browse files
thorn0gkalpak
authored andcommitted
fix(ngMock): pass failed HTTP expectations to $exceptionHandler
This was only partially fixed in f18dd29. Closes #16644
1 parent 864c7f0 commit ebeb1c9

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

src/ngMock/angular-mocks.js

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

1514+
function createFatalError(message) {
1515+
var error = new Error(message);
1516+
// In addition to being converted to a rejection, these errors also need to be passed to
1517+
// the $exceptionHandler and be rethrown (so that the test fails).
1518+
error.$$passToExceptionHandler = true;
1519+
return error;
1520+
}
1521+
15141522
if (expectation && expectation.match(method, url)) {
15151523
if (!expectation.matchData(data)) {
1516-
throw new Error('Expected ' + expectation + ' with different data\n' +
1517-
'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
1524+
throw createFatalError('Expected ' + expectation + ' with different data\n' +
1525+
'EXPECTED: ' + prettyPrint(expectation.data) + '\n' +
1526+
'GOT: ' + data);
15181527
}
15191528

15201529
if (!expectation.matchHeaders(headers)) {
1521-
throw new Error('Expected ' + expectation + ' with different headers\n' +
1522-
'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' +
1523-
prettyPrint(headers));
1530+
throw createFatalError('Expected ' + expectation + ' with different headers\n' +
1531+
'EXPECTED: ' + prettyPrint(expectation.headers) + '\n' +
1532+
'GOT: ' + prettyPrint(headers));
15241533
}
15251534

15261535
expectations.shift();
@@ -1541,20 +1550,17 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
15411550
($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
15421551
} else if (definition.passThrough) {
15431552
originalHttpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers);
1544-
} else throw new Error('No response defined !');
1553+
} else throw createFatalError('No response defined !');
15451554
return;
15461555
}
15471556
}
1548-
var error = wasExpected ?
1549-
new Error('No response defined !') :
1550-
new Error('Unexpected request: ' + method + ' ' + url + '\n' +
1551-
(expectation ? 'Expected ' + expectation : 'No more request expected'));
15521557

1553-
// In addition to be being converted to a rejection, this error also needs to be passed to
1554-
// the $exceptionHandler and be rethrown (so that the test fails).
1555-
error.$$passToExceptionHandler = true;
1558+
if (wasExpected) {
1559+
throw createFatalError('No response defined !');
1560+
}
15561561

1557-
throw error;
1562+
throw createFatalError('Unexpected request: ' + method + ' ' + url + '\n' +
1563+
(expectation ? 'Expected ' + expectation : 'No more request expected'));
15581564
}
15591565

15601566
/**

test/ngMock/angular-mocksSpec.js

+54
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,42 @@ 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 about data when expectations about both data and headers fail', 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 data/);
1533+
});
1534+
1535+
1536+
it('should throw error when response is not defined for a backend definition', function() {
1537+
expect(function() {
1538+
hb.whenGET('/some'); // no .respond(...) !
1539+
hb('GET', '/some', null, callback);
1540+
hb.flush();
1541+
}).toThrowError('No response defined !');
1542+
});
1543+
1544+
15091545
it('should match headers if specified', function() {
15101546
hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1');
15111547
hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2');
@@ -2833,6 +2869,24 @@ describe('ngMockE2E', function() {
28332869
}).toThrowError('Unexpected request: GET /some\nNo more request expected');
28342870
});
28352871

2872+
it('should throw error when expectation fails - without error callback', function() {
2873+
expect(function() {
2874+
hb.expectPOST('/some', { foo: 1 }).respond({});
2875+
$http.post('/some', { foo: 2 }).then(noop);
2876+
2877+
hb.flush();
2878+
}).toThrowError(/^Expected POST \/some with different data/);
2879+
});
2880+
2881+
it('should throw error when unexpected request - with error callback', function() {
2882+
expect(function() {
2883+
hb.expectPOST('/some', { foo: 1 }).respond({});
2884+
$http.post('/some', { foo: 2 }).then(noop, noop);
2885+
2886+
hb.flush();
2887+
}).toThrowError(/^Expected POST \/some with different data/);
2888+
});
2889+
28362890

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

0 commit comments

Comments
 (0)