title: Karma-Jasmine: How to properly test http calls?
body: <p><strong>THE SITUATION:</strong></p>
  
  <p>I need to test an http call and make sure it is called properly.</p>
  
  <p>In the app everything is working fine: $scope.language_list properly retrieve the data from the api</p>
  
  <p>But in the test $scope.language_list is undefined.
  So probably i am not making the test right.</p>
  
  <p><strong>THE CODE:</strong></p>
  
  <p><strong>The function:</strong></p>
  
  <pre><code>$scope.get_language_list = function()
  {
      $http.get('http://my_app.org/languageList')
      .success(function(data, status, headers, config) 
      {
          $scope.language_list = data;
          $scope.valid = true;
  
      })
      .error(function(data, status, headers, config) 
      {
          $scope.valid = false;
      });
  }
  </code></pre>
  
  <p><strong>The test:</strong></p>
  
  <pre><code>describe('Http calls test', function() {
  
      beforeEach(module('my_app.controllers'));
  
      beforeEach(inject(function(_$controller_, _$httpBackend_) 
      {
          $scope = {};
          var controller = $controller('MainCtrl', { $scope: $scope });
  
          $httpBackend = _$httpBackend_;
          $httpBackend.whenGET('http://my_app.org/languageList').respond(200);
  
      }));
  
      it('should load default language list', function() 
      {
          $httpBackend.flush();
  
          console.log($scope.language_list);
  
          expect($scope.valid).toBe(true);
          expect($scope.language_list).not.toEqual(undefined);
      });
  
  
  });
  </code></pre>
  
  <p><strong>THE RESULT:</strong></p>
  
  <p><strong>$scope.valid</strong> is tested fine. If I intentionally change its value in the function to see the test fail it does fail, otherwise is properly passed as true.</p>
  
  <p>But <strong>$scope.language_list</strong> is not working properly, and is passed as undefined. Indeed the error message is: <code>Expected undefined not to equal undefined.</code></p>
  
  <p><strong>THE QUESTION:</strong></p>
  
  <p>How can i properly test an http call?
  Why $scope.language_list is undefined? What I am doing wrong in the test?</p>
  
  <p>Thank you very much!</p>