title: Angularjs unit testing resolving promises
body: <p>I'm probably doing this wrong but I can't figure out how to fix it.</p>
  
  <p>I want to test a controller that uses a resource (ngResource) and I want to use a Spy as a test double for the resource so it doesn't actually do the http call. In the code below I just want to test the search function in the controller.</p>
  
  <p>Controller:</p>
  
  <pre><code>controllers = angular.module('app.controllers');
  controllers.controller('landingCtrl', ['$scope', '$q', 'categoryResource', function ($scope, $q, categoryResource) {
  
      $scope.search = function (text) {
          console.log('searching for: ' + text);
          var deferred = $q.defer();
          categoryResource.query({ searchTerm: text }, function (result) {
              if (result.length == 0) {
                  deferred.resolve(['No results found']);
              } else {
                  deferred.resolve(result);
              }
          });
          return deferred.promise;
      }
  }]);
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>var services = angular.module('app.services');    
  services.factory('categoryResource', ['$resource', function ($resource) {
      var resource = $resource('/api/category');    
      return resource;
  }]);
  </code></pre>
  
  <p>Spec for landingCtrl:</p>
  
  <pre><code>describe('Controller: landingCtrl ', function () {
  
      var $q,
          $rootScope,
          $scope;
  
      beforeEach(module('ngResource'));
      beforeEach(module('app.services'));
      beforeEach(module('app.controllers'));
  
      beforeEach(inject(function (_$rootScope_, _$q_) {
          $q = _$q_;
          $rootScope = _$rootScope_;
      }));
  
      // mock any depencencies, like scope. $resource or $http
      beforeEach(inject(function ($controller, $injector, categoryResource) {
          $scope = $rootScope.$new();
  
          spyOn(categoryResource, 'query').andCallFake(function (searchText) {
              console.log('query fake being called');
              var deferred = $q.defer();
              deferred.resolve(['Test', 'Testing', 'Protester']);
              return deferred.promise;
          });
  
          landingCtrl = $controller('landingCtrl', {
              '$scope': $scope,
              '$q': $q,
              'categoryResource': categoryResource
          });
      }));
  
      afterEach(inject(function ($rootScope) {
          $rootScope.$apply();
      }));
  
      it('should return words with "test" in them"', function () {
          $scope.search('test').then(function (results) {
              console.log(results);
              expect(results).toContain('Test');
          });
          $scope.$apply();
      });
  });
  </code></pre>
  
  <p>The test executes without errors but it passes without ever resolving the promise so my code inside the "then" function never gets called. What am I doing wrong?</p>
  
  <p>I've created a plunker with the above and a test that should fail:</p>
  
  <p><a href="http://plnkr.co/edit/adE6fTajgbDoM33rtbZS?p=preview" rel="nofollow">http://plnkr.co/edit/adE6fTajgbDoM33rtbZS?p=preview</a></p>