angularjs - Cannot access isolate scope of directive in Jasmine test -
i need access isolate scope of directive in order test functions contains. have tried solutions found so. problem in isolate scope of directive, because if don't set directive isolate scope, tests work fine.
my directive looks this:
angular.module('validators').directive('validatename', [function() { var directive = {}; directive.restrict = "a"; directive.require = 'ngmodel'; directive.scope = {}; directive.link = function(scope, element, attrs, ngmodel) { scope.checkifgoodcharactersonly = function(name) { if ( name.match(/[0-9!$%^&*@#()_+|~=`{}\[\]:";'<>?,.\/]/g) ) { return false; } else { return true; } }; }; return directive; }]);
the testing set-up looks this:
beforeeach(module('validators')); describe('directive: validatename', function () { var $scope, elem; beforeeach( inject(function ($compile, $rootscope) { $scope = $rootscope.$new(); elem = angular.element('<form name="form">' + '<input name="name" ng-model="transaction.name" validate-name />' + '</form>'); elem = $compile(elem)($scope); $scope = elem.isolatescope(); })); describe("link", function() { it("should call checkifgoodcharactersonly", function() { expect($scope.checkifgoodcharactersonly("abcd")).toequal(true); }); }); });
the output in console:
typeerror: 'undefined' not object (evaluating '$scope.checkifgoodcharactersonly')
since form element wraps input, attempting access scope of form element, not exist.
this worked:
input = elem.find('input'); expect(input.isolatescope()).tobedefined();
Comments
Post a Comment