{"record":{"id":"02e2d42b90e14a64","repo":"angular/angular.js","slug":"no-more-pending-request-to-flush","errorCode":null,"errorMessage":"No more pending request to flush !","messagePattern":"No more pending request to flush !","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/ngMock/angular-mocks.js","lineNumber":1989,"sourceCode":"   *\n   * If there are no pending requests to flush when the method is called, an exception is thrown (as\n   * this is typically a sign of programming error).\n   *\n   * @param {number=} count - Number of responses to flush. If undefined/null, all pending requests\n   *     (starting after `skip`) will be flushed.\n   * @param {number=} [skip=0] - Number of pending requests to skip. For example, a value of `5`\n   *     would skip the first 5 pending requests and start flushing from the 6th onwards.\n   */\n  $httpBackend.flush = function(count, skip, digest) {\n    if (digest !== false) $rootScope.$digest();\n\n    skip = skip || 0;\n    if (skip >= responses.length) throw new Error('No pending request to flush !');\n\n    if (angular.isDefined(count) && count !== null) {\n      while (count--) {\n        var part = responses.splice(skip, 1);\n        if (!part.length) throw new Error('No more pending request to flush !');\n        part[0]();\n      }\n    } else {\n      while (responses.length > skip) {\n        responses.splice(skip, 1)[0]();\n      }\n    }\n    $httpBackend.verifyNoOutstandingExpectation(digest);\n  };\n\n\n  /**\n   * @ngdoc method\n   * @name $httpBackend#verifyNoOutstandingExpectation\n   * @description\n   * Verifies that all of the requests defined via the `expect` api were made. If any of the\n   * requests were not made, verifyNoOutstandingExpectation throws an exception.\n   *","sourceCodeStart":1971,"sourceCodeEnd":2007,"githubUrl":"https://github.com/angular/angular.js/blob/d8f77817eb5c98dec5317bc3756d1ea1812bcfbe/src/ngMock/angular-mocks.js#L1971-L2007","documentation":"The count variant of $httpBackend.flush(count, skip, digest) executes exactly `count` responses, splicing one at a time from position `skip`. When the queue runs dry mid-loop — splice returns an empty array — it throws 'No more pending request to flush !', i.e., you asked for more responses than pending requests exist.","triggerScenarios":"$httpBackend.flush(3) when only 2 requests are pending; combining skip with a count larger than the remaining responses.length - skip; hardcoded counts going stale after the app code now issues fewer (or already-flushed) requests.","commonSituations":"A view load that used to fire 3 API calls is refactored to batch them into 1–2, and the test still flushes 3; two backend calls merged into one endpoint; an early flush in the same test already consumed part of the queue before the flush(count) call.","solutions":["Omit the count and flush everything: $httpBackend.flush() drains all pending responses.","Bound the count by what is actually pending: $httpBackend.flush(Math.min(n, $http.pendingRequests.length)) when you really need per-response flushing.","Update the stale count to match the current number of requests issued by the code under test."],"exampleFix":"// before\n$httpBackend.flush(3); // throws if only 2 requests pending\n\n// after\n$httpBackend.flush(); // drain all pending responses\n// or, when stepwise flushing is needed:\nvar pending = $http.pendingRequests.length;\n$httpBackend.flush(Math.min(3, pending));","handlingStrategy":"validation","validationCode":"// Bound the flush count by what is actually pending\ninject(function($http, $httpBackend) {\n  var n = Math.min(3, $http.pendingRequests.length);\n  if (n > 0) $httpBackend.flush(n);\n});\n// or simply drain everything: $httpBackend.flush();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid hardcoded counts; flush() without arguments drains all pending responses.","When stepwise flushing is needed, derive the count from $http.pendingRequests.length."],"tags":["angular-mocks","http-backend","flush","testing"],"backgroundTag":"http-mock-no-pending-requests","analyzedSha":"d8f77817eb5c98dec5317bc3756d1ea1812bcfbe","analyzedAt":"2026-08-21T20:36:31.651Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}