From 7bd05f7f99a7cab5640863a2a791d424dd79c57d Mon Sep 17 00:00:00 2001 From: Jacob Hansson Date: Thu, 20 Apr 2017 10:46:31 -0500 Subject: [PATCH 1/3] 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 --- src/ngMock/angular-mocks.js | 8 +++++++- test/ngMock/angular-mocksSpec.js | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index d6e84c75584f..58922d8152a6 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1378,6 +1378,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) { } } + handleResponse.description = method + ' ' + url; return handleResponse; function handleResponse() { @@ -1884,7 +1885,12 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) { $httpBackend.verifyNoOutstandingRequest = function(digest) { if (digest !== false) $rootScope.$digest(); if (responses.length) { - throw new Error('Unflushed requests: ' + responses.length); + var unflushedDescriptions = []; + for (var i = 0, len = responses.length; i < len; ++i) { + unflushedDescriptions.push(responses[i].description); + } + throw new Error('Unflushed requests: \n ' + + unflushedDescriptions.join('\n ')); } }; diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 441509376561..1433dda2eb03 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1678,7 +1678,7 @@ describe('ngMock', function() { expect(function() { hb.verifyNoOutstandingRequest(); - }).toThrowError('Unflushed requests: 1'); + }).toThrowError('Unflushed requests: \n GET /some'); }); @@ -1690,7 +1690,7 @@ describe('ngMock', function() { expect(function() { hb.verifyNoOutstandingRequest(); - }).toThrowError('Unflushed requests: 1'); + }).toThrowError('Unflushed requests: \n GET /some'); })); }); From 19627d7242fefc89465700cf3c14c32e3f186a4b Mon Sep 17 00:00:00 2001 From: Jacob Hansson Date: Thu, 20 Apr 2017 14:58:02 -0500 Subject: [PATCH 2/3] Include number of unflushed requests, add test for multiple unflushed --- src/ngMock/angular-mocks.js | 2 +- test/ngMock/angular-mocksSpec.js | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 58922d8152a6..170ac1b895b8 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1889,7 +1889,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) { for (var i = 0, len = responses.length; i < len; ++i) { unflushedDescriptions.push(responses[i].description); } - throw new Error('Unflushed requests: \n ' + + throw new Error('Unflushed requests: ' + responses.length + '\n ' + unflushedDescriptions.join('\n ')); } }; diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 1433dda2eb03..4bfad9d3bb10 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1678,7 +1678,8 @@ describe('ngMock', function() { expect(function() { hb.verifyNoOutstandingRequest(); - }).toThrowError('Unflushed requests: \n GET /some'); + }).toThrowError('Unflushed requests: 1\n' + + ' GET /some'); }); @@ -1690,8 +1691,23 @@ describe('ngMock', function() { expect(function() { hb.verifyNoOutstandingRequest(); - }).toThrowError('Unflushed requests: \n GET /some'); + }).toThrowError('Unflushed requests: 1\n' + + ' GET /some'); })); + + + it('should describe multiple unflushed requests', function() { + hb.when('GET').respond(200); + hb.when('PUT').respond(200); + hb('GET', '/some', null, noop, {}); + hb('PUT', '/elsewhere', null, noop, {}); + + expect(function() { + hb.verifyNoOutstandingRequest(); + }).toThrowError('Unflushed requests: 2\n' + + ' GET /some\n' + + ' PUT /elsewhere'); + }); }); From ddbcec48434f7bc172831daf60ccaaf64ff21611 Mon Sep 17 00:00:00 2001 From: Jacob Hansson Date: Thu, 20 Apr 2017 15:01:43 -0500 Subject: [PATCH 3/3] Use map instead of for loop for terser approach --- src/ngMock/angular-mocks.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 170ac1b895b8..1d21364da7bb 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1885,10 +1885,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) { $httpBackend.verifyNoOutstandingRequest = function(digest) { if (digest !== false) $rootScope.$digest(); if (responses.length) { - var unflushedDescriptions = []; - for (var i = 0, len = responses.length; i < len; ++i) { - unflushedDescriptions.push(responses[i].description); - } + var unflushedDescriptions = responses.map(function(res) { return res.description; }); throw new Error('Unflushed requests: ' + responses.length + '\n ' + unflushedDescriptions.join('\n ')); }