2014-09-07 4 views
2

사용자 정의 각도 js 지시문을 단위 테스트하려고합니다.jasmine을 사용하여 사용자 정의 지시문을 단위 테스트 할 수 있습니까?

var app = angular.module('myApp', []); 

app.directive('myValidation', function() { 
    return { 
     restrict: 'A', 
     require: 'ngModel', 
     link: function (scope, elem, attr, ctrl) { 
      var regex = new RegExp(attr.myValidation); 
      ctrl.$parsers.unshift(function (value) { 
       var valid = regex.test(value); 
       ctrl.$setValidity('myValidation', valid); 
       console.log('valid?', valid); 
       console.log('value?', value); 
       return valid ? value : undefined; 
      }); 
      ctrl.$formatters.unshift(function (value) { 
       ctrl.$setValidity('regexValidate', regex.test(value)); 
       return value; 
      }); 
     } 
    }; 
}); 

내 사양은 다음과 같습니다 :

describe('Testing myValidation directive', function() { 
    var scope, 
     elem, 
     directive, 
     compiled, 
     html, 
     ctrl; 

    beforeEach(function(){ 
     module('myApp'); 
     html = '<input type="text" ng-model="test" name="test" ng-minlength="50" my-validation="^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d$" />' 
     inject(function($compile, $rootScope,$controller) { 
      scope = $rootScope.$new(); 
      elem = angular.element(html); 
      compiled = $compile(elem); 
      compiled(scope); 
      ctrl=elem.controller('myValidation'); 
      ctrl=$controller; 
      scope.$digest(); 
     }); 
    }); 

    it('Should result in valid datetime validation', function() { 
     var date='27/01/2000'; 

     ctrl.$viewValue=date; 
     scope.$apply(); 

     expect(ctrl.Validity).toBe(true); 
    }); 
}); 

내가 고민하고 비트가 얼마나 '실행'단위 테스트의 지시를에 지시어는 정규 표현식을 기반으로 날짜 확인합니까?

plunkr 심판 : 당신은 그냥 같은 것을 할 수 http://plnkr.co/edit/d4buHW8x7fMh8Hl5JKze?p=preview

답변

2

..

it('Description', function() {  
    elem .val("27/01/2000");  
    elem .trigger("input");  
    $scope.$digest();  
    expect(elem.hasClass('ng-invalid')).toBeTruthy();  
}); 
관련 문제