Skip to content

Commit 080357e

Browse files
jakewinsNarretz
authored andcommitted
feat(ngMock): describe unflushed http requests
The current implementation of $httpBackend.verifyNoOutstandingRequest gives an integer number describing how many requests are unflushed. While it's superficially easy to solve test errors from that message by simply adding an additional $httpBackend.flush(), if a developer is truly not expecting the code to make further requests this is not ideal. This change explicitly prints out which additional requests remain unflushed in the error message, helping her determine if the code needs changing, or if an additional flush is appropriate. Before this change: Unflushed requests: 1 After this change: Unflushed requests: 1 GET /some Closes angular#10596 Closes angular#15928
1 parent 69c3faf commit 080357e

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/ngMock/angular-mocks.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
13781378
}
13791379
}
13801380

1381+
handleResponse.description = method + ' ' + url;
13811382
return handleResponse;
13821383

13831384
function handleResponse() {
@@ -1884,7 +1885,9 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
18841885
$httpBackend.verifyNoOutstandingRequest = function(digest) {
18851886
if (digest !== false) $rootScope.$digest();
18861887
if (responses.length) {
1887-
throw new Error('Unflushed requests: ' + responses.length);
1888+
var unflushedDescriptions = responses.map(function(res) { return res.description; });
1889+
throw new Error('Unflushed requests: ' + responses.length + '\n ' +
1890+
unflushedDescriptions.join('\n '));
18881891
}
18891892
};
18901893

test/ngMock/angular-mocksSpec.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,8 @@ describe('ngMock', function() {
16781678

16791679
expect(function() {
16801680
hb.verifyNoOutstandingRequest();
1681-
}).toThrowError('Unflushed requests: 1');
1681+
}).toThrowError('Unflushed requests: 1\n' +
1682+
' GET /some');
16821683
});
16831684

16841685

@@ -1690,8 +1691,23 @@ describe('ngMock', function() {
16901691

16911692
expect(function() {
16921693
hb.verifyNoOutstandingRequest();
1693-
}).toThrowError('Unflushed requests: 1');
1694+
}).toThrowError('Unflushed requests: 1\n' +
1695+
' GET /some');
16941696
}));
1697+
1698+
1699+
it('should describe multiple unflushed requests', function() {
1700+
hb.when('GET').respond(200);
1701+
hb.when('PUT').respond(200);
1702+
hb('GET', '/some', null, noop, {});
1703+
hb('PUT', '/elsewhere', null, noop, {});
1704+
1705+
expect(function() {
1706+
hb.verifyNoOutstandingRequest();
1707+
}).toThrowError('Unflushed requests: 2\n' +
1708+
' GET /some\n' +
1709+
' PUT /elsewhere');
1710+
});
16951711
});
16961712

16971713

0 commit comments

Comments
 (0)