title: How do I test an $httpBackend POST error state with AngularJS / Karma?
body: <p>I'm trying to write a unit test for my Angular Service and here's a function in the service:</p>
  
  <pre><code>login = function(authObject) {
    deferred = $q.defer();
  
    $http({
      url: '/api/v1/session/create',
      method: 'POST',
      data: authObject
    }).success(function(response) {
      var user;
      if (response.status === 'ok' &amp;&amp; response.user &amp;&amp; response.authenticated === true) {
        user = response.user;
      }
      return deferred.resolve(response);
    }).error(function(data) {
      deferred.reject(data);
      return $state.go('api_error');
    });
    return deferred.promise;
  };
  </code></pre>
  
  <p>I can successfully test the <code>success</code> case with something like:</p>
  
  <pre><code>it('should go to the api error state', function() {
    var authObject;
    authObject = {
      username: 'a@b.com',
      password: 'c'
    };
    $httpBackend.expectPOST('/api/v1/session/create').respond(someData);
  
    userService.login(authObject).then(function(response) {
      return console.log("not error", response);
    }, function(response) {
      return console.log("error", response);
    });
    return expect($state.go).toHaveBeenCalledWith('api_error');
  });
  </code></pre>
  
  <p>That works fine, however if I do:
  <code>$httpBackend.expectPOST('/api/v1/session/create').respond(500, 'error');</code>, then the error case doesn't get called. What am I doing wrong?</p>