title: Test controller with promises with jasmine
body: <p>I'm writing a test for a controller that uses a service.
  This is the controller:</p>
  
  <pre><code>  $scope.loadData = function(){
  UserDataService.query({
          user_id: $routeParams.user_id }
  ).$promise.then (function (data) {
      $scope.labels = data.label;
      $scope.data = data.data;
      $.each($scope.data, function (key, value) {
          $scope.original_keys.push(key);
          $scope.original_data.push(value);
      });
  },
  function (error) {
      $location.path("/404");
  
  });
  };
  $scope.loadData();
  </code></pre>
  
  <p>And this is the service:</p>
  
  <pre><code>  angular.module('services')
  .factory('UserDataService', ['$resource', function ($resource) {
  var hostURL = 'http://localhost:9000';
  
      return $resource( hostURL + '/api/users/:user_id',
          {user_id: '@user_id'}, {
              'query': {method: 'GET'}
  
          });
  
  }]);
  </code></pre>
  
  <p>This is the test I've wrote:</p>
  
  <pre><code>ddescribe('UsersCtrl - Mattia Version', function () {
  
  var scope, location, fakeDataService, fakeEditService, routeParams, UsersCtrl;
  var getDeferred;
  var data={label:["a","b"]};
  beforeEach(function() {
      module('maaperture');
  });
  
  beforeEach(angular.mock.inject(function( $rootScope, $location, $routeParams, $controller, $q) {
      scope = $rootScope.$new();
      location = $location;
      routeParams = $routeParams;
      fakeDataService = {
          query: function() {return $http().then();}
      };
  
      getDeferred = $q.defer();
      getDeferred.resolve(data);
  
      spyOn(fakeDataService, 'query').andReturn(getDeferred);
  
  
      UsersCtrl = $controller('UsersCtrl', {
          '$scope': scope,
          '$location': location,
          'UserDataService': fakeDataService,
          'UserEditService': fakeEditService,
          '$routeParams': routeParams
      });
  }));
  
  it('should call stuff', function () {
      expect(fakeDataService.query).toHaveBeenCalled();
  });
  
  it('should set some data on the scope when successful', function () {
      scope.loadData();
      scope.$apply();
      scope.$digest();
      expect(fakeDataService.get).toHaveBeenCalled();
      expect(scope.data).toEqual(getResponse.data);
  });
  </code></pre>
  
  <p>It should be correct as I've followed a lot of guides, but I keep getting </p>
  
  <pre><code> TypeError: Cannot read property 'then' of undefined
  </code></pre>
  
  <p>Everytime I run it.
  I run my test with karma and jasmine. Can't really figure out why I get this error.</p>