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

Commit 548c0a0

Browse files
committed
feat(ngMocks): 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: GET /some Closes #10596
1 parent 69c3faf commit 548c0a0

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/ngMock/angular-mocks.js

+7-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,12 @@ 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 = [];
1889+
for (var i = 0, len = responses.length; i < len; ++i) {
1890+
unflushedDescriptions.push(responses[i].description);
1891+
}
1892+
throw new Error('Unflushed requests: \n ' +
1893+
unflushedDescriptions.join('\n '));
18881894
}
18891895
};
18901896

test/ngMock/angular-mocksSpec.js

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

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

16841684

@@ -1690,7 +1690,7 @@ describe('ngMock', function() {
16901690

16911691
expect(function() {
16921692
hb.verifyNoOutstandingRequest();
1693-
}).toThrowError('Unflushed requests: 1');
1693+
}).toThrowError('Unflushed requests: \n GET /some');
16941694
}));
16951695
});
16961696

0 commit comments

Comments
 (0)