title: How to write a karma test for a promise
body: <p>I have some code that runs a timer when user is active and broadcasts and event to keep the session alive.</p>
  
  <p>In the controller I have some code to listen for that event and refresh the session. </p>
  
  <p>I want to test this listener</p>
  
  <pre><code>$scope.$on('Keepalive', function () {
     //every 45 minutes make a call to refresh the session.
     var promise = authService.keepAliveSession();
     promise.then(function(userPreferenceData) {
        dataTransfer.setUserPref(userPreferenceData);
     }, function(error) {
        console.log("promise error!!"+error);
     });      
   });
  </code></pre>
  
  <p>my keepalive service returns a promise which will get resolved after the httpbackend returns.</p>
  
  <pre><code>factory.keepAliveSession = function () {
  
    var deferred = $q.defer();
  
    req=...some stuff...
  
    $http(req)
      .success(
         function (data, status) {
            if ( data.user ) {
              // Received data about the logged in user 
               deferred.resolve(factory.userPreferenceData);
            } else {
                // User is not authenticated; redirect to login
                $window.location = data.redirect;
             }
         }
      ).error(function (error) {
          // Error in oAuth refresh service
          deferred.reject("Error in session keepalive"+ error);
      });
  
      return deferred.promise; 
  }
  </code></pre>
  
  <p>here is the test</p>
  
  <pre><code>it('Test for keepalive', function() {
              console.log('starting for keepalive...');
              httpBackend.when('POST', "http://example.com/refreshSession").respond(getMocks().response.oAuthResponseExternal);
  
              spyOn(authServiceMock, "keepAliveSession").and.callThrough();
              spyOn(dataTransferMock, "setUserPref").and.callThrough();
  
              rootScope.$broadcast('Keepalive');
  
              expect(authServiceMock.keepAliveSession).toHaveBeenCalled();
              rootScope.$digest;
              expect(dataTransferMock.setUserPref).toHaveBeenCalled();
  
          });
  </code></pre>
  
  <p>The first assertion is successful, but the second one (after the digest) fails.</p>
  
  <p>How can I force the promise to be resolved or rejected?
  Do I need to somehow mock the $q?</p>