title: Jasmine controller testing, expected spy to have been called
body: <p>I have a method defined in AngularJS controller which is called on initialization. I want to test it using Jasmine (<code>&quot;jasmine-core&quot;: &quot;^2.3.4&quot;, &quot;karma&quot;: &quot;^0.12.37&quot;</code>). I follow some tutorials on the Internet and StackOverflow questions, but I cannot find the right answer. Please take a look at this code:</p>
  <p>Controller <code>usersAddUserController</code>:</p>
  <pre><code>(function () {
      'use strict';
  
      angular.module('app.users.addUser')
          .controller('usersAddUserController', ['$scope', 'usersAddUserService', function ($scope, usersAddUserService) {
  
              usersAddUserService.getCountryPhoneCodes().then(function (phoneCodes) {
                  $scope.phoneCodes = phoneCodes;
              });
  
          }]);
  }());
  </code></pre>
  <p>Jasmine test:</p>
  <pre><code>(function () {
  
      'use strict';
  
      describe('usersAddUserControllerUnitTest', function () {
          var scope, deferred, objectUnderTest, mockedAddUserService;
  
          beforeEach(module('app'));
  
          beforeEach(inject(function ($rootScope, $q, $controller) {
              scope = $rootScope.$new();
  
              function emptyPromise() {
                  deferred = $q.defer();
                  return deferred.promise;
              }
  
              mockedAddUserService = {
                  getCountryPhoneCodes: emptyPromise
              };
                       
              objectUnderTest = $controller('usersAddUserController', {
                  $scope: scope,
                  usersAddUserService: mockedAddUserService
              });
          }));
  
          it('should call getCountryPhoneCodes method on init', function () {
              //when            
              spyOn(mockedAddUserService, 'getCountryPhoneCodes').and.callThrough();
              
              deferred.resolve();
  
              scope.$root.$digest();
              
              //then
              expect(mockedAddUserService.getCountryPhoneCodes).toHaveBeenCalled();
          });
  
      });
  }());
  </code></pre>
  <p>After running the tests, the error message is:</p>
  <blockquote>
  <p>PhantomJS 1.9.8 (Windows 7 0.0.0) usersAddUserControllerUnitTest should call getCountryPhoneCodes method on init FAILED</p>
  <pre><code>    Expected spy getCountryPhoneCodes to have been called.
  </code></pre>
  </blockquote>
  <p>I obviously missing something, but I cannot figure out what it is. Any help will be appreciated.</p>