title: AngularJS Service Jasmine Unit Test Unflushed Request Error
body: <p>I'm writing my first unit test to check the functionality of a AngularJS service. This is the part of the service with the <code>getList</code> function I'm testing:</p>
  
  <pre><code>(function() {
  'use strict';
  
  angular
      .module('app.services')
      .factory('RestService', RestService);
  
      RestService.$inject = [ '$http' ];
  
      function RestService($http) { 
  
          var service = {
              getList: getList,
              ...
          };
          return service;
  
          function getList(url) {
              return $http.get(url)
              .then(function(response) {
                  console.log('RestService: got list: ' + response.data.length);
                  return response.data;
              })
              .catch(function(errResponse) {
                  console.error('RestService: error getting list: ' + errResponse.status);       
              });
          }
  </code></pre>
  
  <p>And I've written the following unit test:</p>
  
  <pre><code>'use strict';
  
  describe('Test Rest Service', function() {
  
  var httpBackend;
  var RestService;
  var reqHandler;
  
  beforeEach(module('app.services'));
  
  beforeEach(inject(function ($httpBackend, _RestService_) {
      httpBackend = $httpBackend;
      RestService = _RestService_;
      reqHandler = httpBackend.when('GET', '/api/events')
                      .respond(200, '[{ "event": 1 }, { "event": 2 }]');
  }));
  
  afterEach(function() {
      httpBackend.verifyNoOutstandingExpectation();
      httpBackend.verifyNoOutstandingRequest();
  });
  
  it('should get a list of events', function() {
  
      var url = '/api/events';
      httpBackend.expectGET(url);
      RestService.getList(url).then(function(response) {
          httpBackend.flush();
          expect(response.length).toEqual(2);
      });
  });
  });
  </code></pre>
  
  <p>When I run the test I get the following error:</p>
  
  <pre><code>Test Rest Service
  PhantomJS 1.9.8 (Windows 8 0.0.0) Test Rest Service "after each" hook FAILED
      Error: Unflushed requests: 1
  </code></pre>
  
  <p>My first guess is that then <code>then</code> part of the test is not executing but I can't find a way to check this. Can anyone explain why I have an unflushed request? Also, is there some way I can step through the test or at least print the variables to screen so I can figure out what's going on? </p>
  
  <p>Thanks in advance! </p>