title: karma mock promise response
body: <p>I have the following factory that I'd like to test:</p>
  
  <pre><code>angular
      .module('enigma.authFactory', [])    
      .factory('authFactory', authFactory);
  
  authFactory.$inject = ['$http'];
  
  function authFactory($http){
      function doesUserExist(email){
          return $http.post('/doesUserExist', email)
              .success(function(data){
                  if(data !== 'user exists'){
                      return false;
                  } else {
                      return true;
                  }
              });
      }
  }
  </code></pre>
  
  <p>So I wrote the following test:</p>
  
  <pre><code>describe('Auth Service Tests', function() {
      var $httpBackend, defer, doesUserExistReqHandler;
  
      beforeEach(inject(function(_$httpBackend_, $injector, $q) {
          $httpBackend = _$httpBackend_; 
          defer = $q.defer();
          doesUserExistReqHandler = $httpBackend.when('POST', '/doesUserExist').respond(defer.promise);
      }));
  
      describe('authFactory.doesUserExist()', function() {
          it('should return true is a user exists', function() {
              user = {
                  email: 'bwayne@wayneenterprise.com'
              };
              $httpBackend.whenPOST('/doesUserExist', user).respond('user exists');
              var doesUserExist = authFactory.doesUserExist(user);
              $httpBackend.flush();
              expect(doesUserExist).toEqual(true);
          });
      });
  });
  </code></pre>
  
  <p>I checked inside the authFactory.doesUserExist function and I am correctly getting the data set to 'user exists' which routes the function to return true. However in the unit test authFactory.doesUserExist is being set to the following object.</p>
  
  <pre><code>Expected Object({ $$state: Object({ status: 1, pending: undefined, value: Object({ data: Object({ $$state: Object({ status: 0 }) }), status: 200, headers: Function, config: Object({ method: 'POST', transformRequest: [ Function ], transformResponse: [ Function ], paramSerializer: Function, url: '/doesUserExist', data: Object({ email: 'bwayne@wayneenterprise.com' }), headers: Object({ Accept: 'application/json, text/plain, */*', Content-Type: 'application/json;charset=utf-8' }) }), statusText: '' }), processScheduled: false }), success: Function, error: Function }) to equal true.
  </code></pre>
  
  <p>I'm thinking the issue is that the test isn't resolving the promise properly and so I'm setting the res variable before authFactory.doesUserExist has returned true.</p>
  
  <p>How would I go about fixing this?</p>