title: Jasmine unit test asynchronous controller method
body: <p>I'm using Jasmine to unit test an Angular controller which has a method that runs asynchronously. I was able to successfully inject dependencies into the controller but I had to change up my approach to deal with the async because my test would run before the data was loaded. I'm currently trying to spy on the mock dependency and use <code>andCallThrough()</code> but it's causing the error <code>TypeError: undefined is not a function</code>.</p>
  
  <p>Here's my controller...</p>
  
  <pre><code>myApp.controller('myController', function($scope, users) {
      $scope.user = {};
  
      users.current.get().then(function(user) {
          $scope.user = user;
      });
  }); 
  </code></pre>
  
  <p>and my test.js...</p>
  
  <pre><code>describe('myController', function () {
      var scope, createController, mockUsers, deferred;
      beforeEach(module("myApp"));
  
      beforeEach(inject(function ($rootScope, $controller, $q) {
          mockUsers = {
              current: {
                  get: function () {
                      deferred = $q.defer();
                      return deferred.promise;
                  }
              }
          };
          spyOn(mockUsers.current, 'get').andCallThrough();
  
          scope = $rootScope.$new();
          createController = function () {
              return $controller('myController', {
                  $scope: scope,
                  users: mockUsers
              });
          };
      }));
  
  
      it('should work', function () {
          var ctrl = createController();          
          deferred.resolve('me');
          scope.$digest();
          expect(mockUsers.current.get).toHaveBeenCalled();
          expect(scope.user).toBe('me');           
      });
  });
  </code></pre>
  
  <p>If there is a better approach to this type of testing please let me know, thank you.</p>