title: Jasmine test a promise.then function
body: <p>I try to test my app with Jasmine and got the following problem:<br>
  I will calculate something in the <code>then</code> function of my promise. That's the point where I need to test my code.   </p>
  
  <p>Here is the code of my controller:</p>
  
  <pre><code>  TestCtrl.$inject = ["$scope", "TestService"];
    /* ngInject */
    function TestCtrl($scope, TestService) {
      $scope.loadData = function () {
        TestService.getData().then(function (response) {
          $scope.data = response.data;
          $scope.filtered = $scope.data.filter(function(item){
            if(item.id &gt; 1000){
              return true;
            }
            return false;
          })
        });
      }
    }
  </code></pre>
  
  <p>And my Jasmine test code:</p>
  
  <pre><code>describe('TestService tests', function () {
    var $q;
    beforeEach(function () {
      module('pilot.fw.user');
    });
    beforeEach(inject(function (_$q_) {
      $q = _$q_;
    }));
    describe('UserController Tests', function () {
  
      beforeEach(inject(function (_$httpBackend_, $rootScope, $controller) {
        this.scope = $rootScope.$new();
        this.$rootscope = $rootScope;
        this.$httpBackend = _$httpBackend_;
        this.scope = $rootScope.$new();
        var TestServiceMock = {
          getData: function () {
            var deferred = $q.defer();
            var result = [{
              "id": 1720,
              "user": 1132
            },
              {
                "id": 720,
                "user": 132
              }, {
                "id": 1721,
                "user": 1132
              }];
            deferred.promise.data = result;
            deferred.resolve(result);
            return deferred.promise;
          }
        };
        this.controller = $controller('TestCtrl', {
          '$scope': this.scope,
          'TestService': TestServiceMock
        });
      }));
  
      it('test', function(){
        this.scope.loadData();
        expect(true).toBeTruthy();
      })
    });
  });
  </code></pre>
  
  <p>The strange thing I don't understand is (tested with console logs):   </p>
  
  <ul>
  <li>My promise is created and returned</li>
  <li>My loadData function is called and it will call the getData() function from the TestService</li>
  <li>Everything inside the then function won't be executed although I return the promise as resolved</li>
  </ul>
  
  <p>So how could I test the code inside the <strong><em>then</em></strong> function?<br>
  Thanks for help</p>