title: How to properly test Angular's $http success and error promises with Jasmine
body: <p>I have such controller:</p>
  
  <pre><code>$scope.signIn = (user) -&gt;
        if user.email != '' and user.password != ''
          UserService.signIn(user)
  </code></pre>
  
  <p>I have such service:</p>
  
  <pre><code> signIn: (user) -&gt;
          $http.post '/user/signin', user
  
          .success (data) -&gt;
            AuthService.set 'email', data.user.local.email
  
          .success () -&gt;
            $state.go 'auth.profile'
  
          .error () -&gt;
            throw Error(err)
  </code></pre>
  
  <p>I have such test so far:</p>
  
  <pre><code>describe 'AuthController Unit Tests', -&gt;
  
    beforeEach module('kreditorenApp')
  
    AuthController  = undefined
    scope           = undefined
    AuthService     = undefined
    UserService     = undefined
    $httpBackend    = undefined
    $controller     = undefined
    $location       = undefined
    user            = undefined
    HTTPInterceptor = undefined
    $alert          = undefined
  
    resUser =
      "status": "OK"
      "error": "Erfolgreich Authentifiztiert"
      "type": "success"
      "token": "TOKEN STRING"
      "user":
        "__v": 0
        "_id": "5512a0dfcbc8ea974647f493"
        "local":
          "password": "HASH STRING"
          "email": "dieter"
  
  
    beforeEach inject(($rootScope, _$controller_, _$httpBackend_, _UserService_, _AuthService_, _$location_, _$alert_, _HTTPInterceptor_) -&gt;
  
      scope         = $rootScope.$new()
      $controller   = _$controller_
      $httpBackend  = _$httpBackend_
      $location     = _$location_
      UserService   = _UserService_
      AuthService   = _AuthService_
      HTTPInterceptor = _HTTPInterceptor_
  
      # Stubs
      #
      httpFake = () -&gt;
        bla:
          "bla"
  
      userFake = () -&gt;
        signIn:
          "SUCCESS"
  
      # Spies
      #
      spyOn(HTTPInterceptor, 'response').and.callFake(httpFake)
      # spyOn(UserService, 'signIn').and.callFake(userFake)
  
      $httpBackend.expectPOST('/user/signin', user).respond(200, {user":"local":"email":"emperorRudolf"})
      $httpBackend.expectGET('app/controller/auth/auth.html').respond(200)
      $httpBackend.expectGET('app/controller/auth/partials/profile.html').respond(200)
  
      AuthController = $controller('AuthController', $scope:scope)
    )
  
    afterEach -&gt;
      $httpBackend.verifyNoOutstandingExpectation()
      $httpBackend.verifyNoOutstandingRequest()
  
    it 'should signIn user &amp; ui-route to auth.profile &amp; invoke HTTPInterceptor', -&gt;
  
      # Tests
      #
      scope.signIn ({email:'mail', password:'pass'})
      $httpBackend.flush()
  
      #expect(HTTPInterceptor.response).toHaveBeenCalled
      #expect many more things later
  </code></pre>
  
  <p>The test is failing with</p>
  
  <pre><code> TypeError: 'undefined' is not an object (evaluating 'data.user')
  </code></pre>
  
  <p>I want to mock out as few as possible. </p>
  
  <p><em>Edit:</em> The data object is always undefined. <code>$http</code>'s rather unique <code>.success</code> method is not accessible or known to my tests. How do I test this properly? </p>