title: How to check if a function is available in instantiated controller for unit testing in Jasmine
body: <p>I am trying to unittest a controller in AngularJS for the first time. I want to test if the login-function in this controller calls the right service but I get the error, that the function doesn't exist in the controller. What did I do wrong? Do I have to mock more? But I shouldn't have to mock the functions in the controller to be tested because if I would do that the whole testing would be senseless, or am I wrong?</p>
  
  <p>Controller-Snippet:</p>
  
  <pre><code>function LoginController($scope, $state, myService, ngToast) {
  
      $scope.loginUser = loginUser;
  
      activate();
      function activate() {}
  
      function loginUser(credentials) {
         myService.authenticate(/*things*/);
      }
  }
  </code></pre>
  
  <p>Test-Code:</p>
  
  <pre><code>describe('Login Controller', function () {
  'use strict';
  
  //globals
  var controller, scope;
  var $controller, $state, myService, ngToast;
  
  //needed modules
  beforeEach(module('app.login'));
  beforeEach(module('app.core'));    
  beforeEach(module('ui.router'));
  
  //instanciate Controller        
  beforeEach(inject(function (_$controller_, _$state_, _myService_,  _ngToast_) {
      scope = {};
      $state = _$state_;
      myService = _myService_;
      ngToast = _ngToast_;
  
      $controller = _$controller_;
  
      controller = $controller('LoginCtrl', {
          $scope: scope,
          $state: $state,
          myService: myService,
          ngToast: ngToast
      });
  }));
  
  it('should have an existing controller', function () {
      expect(controller).toBeDefined();
  });
  
  /*************************** unit-tests ***************************/
  describe('.loginUser()', function () {
      it('should exist', function () {
          expect(controller.loginUser).toBeDefined();
      });
  });
  });
  </code></pre>
  
  <p>The Error I am getting when running karma start:</p>
  
  <pre><code>.loginUser()
    ✗ should exist
  TypeError: controller.loginUser is not a function
      at Object.&lt;anonymous&gt; (src/login/login.controller.spec.js:74:31)
  </code></pre>
  
  <p>But it seems to me that the controller does exist, because this test doesn't fail:</p>
  
  <pre><code>Login Controller
   ✓ should have an existing controller
  </code></pre>