{"record":{"id":"e2a69bfcdca09f60","repo":"angular/angular.js","slug":"no-pending-request-to-flush","errorCode":null,"errorMessage":"No pending request to flush !","messagePattern":"No pending request to flush !","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/ngMock/angular-mocks.js","lineNumber":1984,"sourceCode":"   * @description\n   * Flushes pending requests using the trained responses. Requests are flushed in the order they\n   * were made, but it is also possible to skip one or more requests (for example to have them\n   * flushed later). This is useful for simulating scenarios where responses arrive from the server\n   * in any order.\n   *\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","sourceCodeStart":1966,"sourceCodeEnd":2002,"githubUrl":"https://github.com/angular/angular.js/blob/d8f77817eb5c98dec5317bc3756d1ea1812bcfbe/src/ngMock/angular-mocks.js#L1966-L2002","documentation":"The fake $httpBackend queues one response entry per request actually made against its expectations/definitions; flush([count][, skip][, digest]) executes them. It throws 'No pending request to flush !' when there is nothing to execute from the requested position — responses is empty, or skip >= responses.length — meaning no (more) HTTP request was made by the code under test.","triggerScenarios":"Calling $httpBackend.flush() before the code under test performed any $http/$resource call; calling flush twice when the first flush consumed all responses; using flush(count, skip) with skip pointing past the end of the queue.","commonSituations":"The test forgot to invoke the controller/service function that issues the request; the request only starts inside a callback that has not run yet (e.g., inside a $timeout — flush() digests first but does not run timers); an earlier exception in the test prevented the $http call; all requests were already flushed by a previous flush in a multi-step test.","solutions":["Trigger the request first: call the controller/service method (or run the event handler) that performs $http/$resource before flush().","If the request is gated behind a $timeout or promise chain, run that step ($timeout.flush()) before $httpBackend.flush().","Guard the call: check $http.pendingRequests.length > 0 before flushing.","In multi-flush tests, track how many responses remain and use verifyNoOutstandingRequest() to assert the end state instead of flushing blindly."],"exampleFix":"// before\nit('loads users', inject(function($httpBackend, $controller) {\n  $httpBackend.expectGET('/users').respond([]);\n  $httpBackend.flush(); // throws: no request made yet\n  var ctrl = $controller('UserListCtrl');\n}));\n\n// after\nit('loads users', inject(function($httpBackend, $controller) {\n  $httpBackend.expectGET('/users').respond([]);\n  var ctrl = $controller('UserListCtrl'); // constructor issues $http.get\n  $httpBackend.flush();\n}));","handlingStrategy":"validation","validationCode":"// Flush only when requests are actually in flight\ninject(function($http, $httpBackend) {\n  if ($http.pendingRequests.length > 0) {\n    $httpBackend.flush();\n  }\n});","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call the controller/service method that issues $http before flush().","If the request is gated behind $timeout/promise steps, flush those first.","In afterEach use verifyNoOutstandingRequest()/verifyNoOutstandingExpectation() instead of extra flushes."],"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"}