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.
Directive
(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));
Directive Unit Test
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('<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?'); }); }); });
I tried following these instructions: http://jsfiddle.net/mazan_robert/hdsjbm9n/
To be able to call methods on the ngModelController, like; $setViewValue.
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.
TypeError: undefined is not an object (evaluating 'ngModelCtrl.$setViewValue')
Thoughts?
Thanks so much for your help!