title: TypeError: Cannot read property 'then' of undefined angularjs-grunt test
body: <p>I'm using $q service to make an async calls. I can't resolve 'then' and 'defer' in unit tests using karma.</p>
  
  <p>The below is my controller code.</p>
  
  <pre><code>scope.filterUrls = [{url:'page1'}, {url: 'page2'}, {url:'page-error'}];
  
  scope.bindFilters = function () {
  angular.forEach(scope.filterUrls, function (data) {
      scope.getFilterData(data.url, '').then(function (result) {
        if (data.url === 'page1') {
          scope.moduleData.index = result.data;
        } else if (data.url === 'page2') {
          scope.moduleData.page2 = result.data;
        } 
       });
    });
  }
  
  scope.getFilterData = function (filterUrls, params) {
    // $q service object
    var deferred = q.defer();
  
    // regular ajax request
    http({
      method: 'GET',
      url: app.api.root + filterUrls,
      params: params
    })
        .success(function (result) {
          // promise resolve
          deferred.resolve(result);
        })
        .error(function (result) {
          // called asynchronously if an error occurs
          // or server returns response with an error status.
          deferred.reject('Erreur request : ' + result);
        });
    return deferred.promise;
  };
  </code></pre>
  
  <p>test spec:</p>
  
  <pre><code>it('should call getFilterData() in bindFilters()', function () {
      spyOn(scope, 'getFilterData');
      scope.bindFilters();
      expect(scope.getFilterData).toHaveBeenCalled();
    });
  </code></pre>
  
  <p>I'm getting an error called "TypeError: Cannot read property 'then' of undefined".</p>
  
  <p>How can I write an unit test for those two methods using karma.</p>
  
  <p>Update:</p>
  
  <p>1.how do we can test the success and error of scope.getFilterData()</p>
  
  <p>2 .then function in scope.bindFilters() function.</p>
  
  <p>Please help..</p>