title: Cannot read property of undefined in angular karma test
body: <p>I am testing a service function which makes numerous <code>$http.get()</code> calls. The actual function under test returns a promise. Currently, the test is failing with <code>response is undefined</code>.</p>
  
  <p>Here is the test:</p>
  
  <pre><code>it('should return the list of catalogues', inject(function ($q, bookService) {
      var list;
      var deferred = $q.defer();
      var promise = deferred.promise;
  
      promise.then(function (response) {
          list = response.success; // Cannot read property 'success' of undefined
      });
  
      bookService.getCatalogues().then(function (response) {
          deferred.resolve(response); // this line is hit first
      });
  
      $httpBackend.flush();
  
      expect(list).toEqual(listOfBooks); // listOfBooks is defined outside test
  }));
  </code></pre>
  
  <p>What am I doing wrong?</p>