Skip to content

Commit 267ee9c

Browse files
katemihalikovagkalpak
authored andcommitted
fix(ngMock): trigger digest in $httpBackend.verifyNoOutstandingRequest()
Firing a digest at the beginning of the verification process, ensures that requests fired asynchronously - e.g. in the `resolve` handler of a promise as happens with `$http` - will get detected as well. Previously, in order to reliably verify that there was no outstanding request, users needed to manually trigger a digest, before calling `verifyNoOutstandingRequest()`. Failing to do so, could prevent `verifyNoOutstandingRequest()` from detecting pending requests and cover bugs in application code. This is no longer the case, since a digest will always be fired autommatically as part of a call to `verifyNoOutstandingRequest()`. Fixes angular#13506 Closes angular#13513 BREAKING CHANGE: Calling `$httpBackend.verifyNoOutstandingRequest()` will trigger a digest. This will ensure that requests fired asynchronously will also be detected (without the need to manually trigger a digest). This is not expected to affect the majority of test-suites. Most of the time, a digest is (directly or indirectly) triggered anyway, before calling `verifyNoOutstandingRequest()`. In the unlikely case that a test needs to verify the timing of a request with respect to the digest cycle, you should rely on other means, such as mocking and/or spying.
1 parent 859b1e3 commit 267ee9c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/ngMock/angular-mocks.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
18471847
* afterEach($httpBackend.verifyNoOutstandingRequest);
18481848
* ```
18491849
*/
1850-
$httpBackend.verifyNoOutstandingRequest = function() {
1850+
$httpBackend.verifyNoOutstandingRequest = function(digest) {
1851+
if (digest !== false) $rootScope.$digest();
18511852
if (responses.length) {
18521853
throw new Error('Unflushed requests: ' + responses.length);
18531854
}

test/ngMock/angular-mocksSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,7 @@ describe('ngMock', function() {
10121012
hb = $httpBackend;
10131013
}));
10141014

1015+
10151016
it('should provide "expect" methods for each HTTP verb', function() {
10161017
expect(typeof hb.expectGET).toBe("function");
10171018
expect(typeof hb.expectPOST).toBe("function");
@@ -1031,6 +1032,7 @@ describe('ngMock', function() {
10311032
expect(typeof hb.whenHEAD).toBe("function");
10321033
});
10331034

1035+
10341036
it('should provide "route" shortcuts for expect and when', function() {
10351037
expect(typeof hb.whenRoute).toBe("function");
10361038
expect(typeof hb.expectRoute).toBe("function");
@@ -1653,6 +1655,7 @@ describe('ngMock', function() {
16531655
});
16541656
});
16551657

1658+
16561659
describe('verifyRequests', function() {
16571660

16581661
it('should throw exception if not all requests were flushed', function() {
@@ -1663,6 +1666,18 @@ describe('ngMock', function() {
16631666
hb.verifyNoOutstandingRequest();
16641667
}).toThrowError('Unflushed requests: 1');
16651668
});
1669+
1670+
1671+
it('should verify requests fired asynchronously', inject(function($q) {
1672+
hb.when('GET').respond(200);
1673+
$q.resolve().then(function() {
1674+
hb('GET', '/some', null, noop, {});
1675+
});
1676+
1677+
expect(function() {
1678+
hb.verifyNoOutstandingRequest();
1679+
}).toThrowError('Unflushed requests: 1');
1680+
}));
16661681
});
16671682

16681683

@@ -1723,6 +1738,7 @@ describe('ngMock', function() {
17231738
});
17241739
});
17251740

1741+
17261742
describe('expectRoute/whenRoute shortcuts', function() {
17271743
angular.forEach(['expectRoute', 'whenRoute'], function(routeShortcut) {
17281744
var methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'JSONP'];
@@ -1753,6 +1769,7 @@ describe('ngMock', function() {
17531769
});
17541770
});
17551771

1772+
17561773
describe('MockHttpExpectation', function() {
17571774
/* global MockHttpExpectation */
17581775

0 commit comments

Comments
 (0)