单元测试自定义指令。密码验证。 AngularJS

时间:2015-02-03 09:33:39

标签: angularjs unit-testing angularjs-directive passwords karma-runner

我有这个AngularJs指令,我需要使用karma-jasmine测试器对此进行单元测试。该指令匹配两个输入字段,并在两个输入文本匹配时将表单设置为有效。 (密码验证器)



angular.module('UserValidation', []).directive('validPasswordC', function () {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {
            ctrl.$parsers.unshift(function (viewValue, $scope) {
                var noMatch = viewValue != scope.myForm.password.$viewValue;
                ctrl.$setValidity('noMatch', !noMatch)
            })
        }
    }
});




这是我的HTML表单



  <form name='myForm'>
      <!-- other stuff  -->
        
    <label>Password</label><br/>
    <input type='password'ng-model='password' name='password' required  ng-minlength="8" ng-maxlength="20" ng-pattern="/(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])(?=.*[!@#$%^&*><.+_])/" />
    <label for="password_c">Confirm Password</label>
    <input type="password" id="password_c" name="password_c" ng-model="formData.password_c" valid-password-c required  />
    
    <!-- other stuff here -->
          
     <button ng-click='adduser()' class="btn btn-default" ng-disabled="!myForm.$valid"> Add User </button>
        
    </form>
&#13;
&#13;
&#13;

这是我试过的TEST案例

&#13;
&#13;
describe("should match the password", function () {
   var scope, myform;
   beforeEach(module('UserValidation'));
   beforeEach(inject(function ($compile, $rootScope) {
    scope=$rootScope;
    var element = angular.element(
        '<form name="myForm">' +
       '<input type="password" id="password_c" name="password_c" ng-model="formData.password_c" valid-password-c required  />' +
       '</form>'
    );
       scope.model = {some: null };
       $compile(element)(scope);
       scope.$digest();
       myform = scope.myForm;
   }));
    it('should validate the directive', function () {
       //how do i test this when it actually validates two input fields
      // i cant seem to find out how, PS im new to angular
    })



});
&#13;
&#13;
&#13;

0 个答案:

没有答案