2016-05-17 86 views
1

我已经使用,增加了我输入的验证一个指令通过NG-消息:AngularJS自定义验证指令单元测试:模型值不确定

angular.module('app.module.submodule').directive('dateFormatFr', function() { 
    return { 
     require: 'ngModel', 
     link: linkValidator 
    }; 
}); 

function linkValidator(scope, elm, attrs, ctrl) { 
    ctrl.$validators.datefr = function (modelValue) { 
     if (ctrl.$isEmpty(modelValue)) { 
      return true; 
     } 
     if (!(modelValue instanceof Date)) { 
// this check avoid errors on datepickers 
      return modelValue.match('^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$'); 
     } 
     return true; 
    }; 
} 

而且我试图单元测试(使用NG-描述):

ngDescribe({ 
    name: 'linkValidator', 
    modules: 'app.module.submodule', 
    inject: [], 
    mocks: {}, 
    only: true, 
    tests: function() { 
     var $compile, 
      $scope, 
      form; 

     beforeEach(inject(function (_$compile_, _$rootScope_) { 
      // The injector unwraps the underscores (_) from around the parameter names when matching (Angular docs) 
      $compile = _$compile_; 
      $scope = _$rootScope_; 
      var tpl = angular.element(
       '<form name="form">' + 
       '<input ng-model="model.date" name="dateField" date-format-fr />' + 
       '</form>' 
      ); 
      $scope.model = { 
       date: null 
      }; 
      $compile(tpl)($scope); 
      form = $scope.form; 
     })); 
     it('should be valid when the field value is well formatted (DD/MM/AAAA)', function() { 
      form.dateField.$setViewValue('01/01/2001'); 
      $scope.$digest(); 
      expect(form.dateField.$valid).toBe(true); 
      expect($scope.model.date).toEqual('01/01/2001'); 
     }); 
     it('should be invalid when date is badly formatted', function() { 
      form.dateField.$setViewValue('30/02/2001'); 
      $scope.$digest(); 
      expect(form.dateField.$valid).toBe(false); 
      expect($scope.model.date).toBeUndefined(); 
     }); 
    } 
}); 

但有2个问题(其可以具有相同的原因??):

  1. 第一测试失败BEC请使用$scope.model.dateundefined(它应该更新为'01/01/2001',因为它是有效的输入)。我注意到form.dateField$modelValue也是未定义的。我知道$scope.model.date已更新,因为如果不会,它将是null。但为什么undefined
  2. 第二次测试失败,因为$valid停留true(应更新为false,因为它无效)。我再次问:为什么?

我试图将我的代码与this post answer进行比较,但我看不出我的错误在哪里。

请注意,该指令在我的应用程序表单中运行良好(模型正在按预期进行更新,并且ng消息也起作用),所以我不知道我在这里做错了什么。

我与角1.4.7工作,如果它可以帮助..

编辑:

试图this method具有相同的结果。 $modeValue保持未定义,而$viewValue$$rawModelValue用正确的字符串更新。

这些都是从我的测试日志:

console.log($scope.localModel); 
// {date:null} before digest 
// {date: undefined} after digest 
console.log(form.dateField.$viewValue); 
// NaN before digest 
// '01/01/2001' after digest 
console.log(form.dateField.$modelValue); 
// NaN before digest 
// undefined after digest 
console.log(form.dateField.$$rawModelValue); 
// undefined before digest 
// '01/01/2001' after digest 

回答

0

我的坏:我通过一个字符串,而不是一个RegExp ...

我也换成了这一个return语句:

return /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/.test(modelValue); 

发现只检查一个条目时使用“测试”方法更有效。

对不起,花了时间在上面。