title: Jasmine - unit testing a directive: Getting 'undefined' is not an object
body: <p>I am taking baby steps in Jasmine, please bear with me for any blatant mistakes..I am writing test cases to check if a controller method - transformData gets called, the details below</p>
  
  <p><strong>My Directive</strong></p>
  
  <pre><code>angular.module('myModule')
  .directive('myDirective', [ function () 
  {
      'use strict';
      return {
          restrict: 'E',
          templateUrl: '/static/quality/scripts/directives/hh-star-rating.html',
          scope: {
              varA:'@',
          },
          controller: [
              '$scope', '$controller',
              function ($scope, $controller) {
                  $controller('otherController', {$scope: $scope})
                      .columns(
                          $scope.x,
                          $scope.y,
                          $scope.series
                      );
                      $scope.transformData =  function(data)
                      {
                          /// does something;
  
                          return data;
                      };
              }
          ],
  </code></pre>
  
  <p><strong>My Spec</strong></p>
  
  <pre><code>describe('directive - hh-star-ratings', function() {
  'use strict';
  angular.module('myModule', [])
  .directive('myContainer', function() {
      return {
          restrict: 'E',
          priority: 100000,
          terminal: true,
          template: '&lt;div&gt;&lt;/div&gt;',
          controller: function($scope) {
              $scope.loadingData = true;
              this.stopLoading = function() {
                  $scope.loadingData = false;
              };
          }
      }
  });
  
  var result_set = {
      //some data-transform-req
  };
  
  beforeEach(module('myModule'));
  
  var element, scope, controller;
  
    beforeEach(inject(function($rootScope, $compile) {
  
        scope = $rootScope.$new();
  
        element = angular.element(
          '&lt;my-container&gt; &lt;my-directive&gt; &lt;/my-directive&gt; &lt;/my-container&gt;'
      );
  
      $compile(element)(scope);
  
      scope.$digest();
  
      controller = element.controller('myDirective');
  
    }));
  
  it('should call the transformData', function() {
      controller.transformData(result_set);
      scope.$apply();
      scope.transformData.should.have.been.called;
  });
  </code></pre>
  
  <p>})</p>
  
  <p><strong>Issue</strong>: When I run the test, I get the following error</p>
  
  <blockquote>
    <p>TypeError: 'undefined' is not an object (evaluating 'controller.transformData')</p>
  </blockquote>
  
  <p>What am I doing wrong? Thanks in advance for your time.</p>