title: Unit Testing Angular Attribute Directive
body: <p>I am having some problems testing an AngularJS (1.5) Attribute restricted Directive. See the below example directive and following unit test, of which produces a broken unit test.</p>
  
  <p><strong>Directive</strong></p>
  
  <pre><code>(function (angular) {
    'use strict';
  
    function SomethingCtrl($filter) {
      return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ngModelController) {
          var exampleFilter = $filter('exampleFilter');
          ngModelController.$parsers.push(function(value) {
            ngModelController.$setViewValue(value);
            ngModelController.$render();
            return value;
          });
        }
      };
    }
  
    SomethingCtrl.$inject = ['$filter'];
  
    angular.module('something.formatter', [
      'filters'
    ]).directive('somethingFormatter', SomethingCtrl);
  }(window.angular));
  </code></pre>
  
  <p><strong>Directive Unit Test</strong></p>
  
  <pre><code>fdescribe('something.formatter spec', function () {
    'use strict';
    var scope,
        element,
        testValue,
        compile,
        ngModelCtrl;
  
    beforeEach(function () {
      module('something.formatter');
  
      compile = function () {
        inject([
          '$compile',
          '$rootScope',
          function ($compile, $rootScope) {
            scope = $rootScope.$new();
            scope.testValue = testValue;
            element = angular.element('&lt;input something-formatter ng-model="testValue"');
            $compile(element)(scope);
            ngModelCtrl = element.controller('ngModel');
            scope.$digest();
          }
        ]);
      };
    });
    describe('initialization', function () {
      beforeEach(function () {
        testValue = 'Yay!';
        compile();
        ngModelCtrl.$setViewValue('Nay?');
      });
      it('should be defined', function () {
        expect(scope.testValue).toEqual('Nay?');
      });
    });
  });
  </code></pre>
  
  <p>I tried following these instructions: <a href="http://jsfiddle.net/mazan_robert/hdsjbm9n/" rel="nofollow noreferrer">http://jsfiddle.net/mazan_robert/hdsjbm9n/</a></p>
  
  <p>To be able to call methods on the ngModelController, like; $setViewValue.</p>
  
  <p>Yet, Jasmine continues to scream at me and tells me that $setViewValue is not a constructor, as well as doesn't even hit console.logs inside the actual directive.</p>
  
  <pre><code>TypeError: undefined is not an object (evaluating 'ngModelCtrl.$setViewValue')
  </code></pre>
  
  <p>Thoughts?</p>
  
  <p>Thanks so much for your help!</p>