AngularJS自定义验证指令单元测试:模型值未定义

时间:2016-05-17 17:21:11

标签: javascript angularjs validation unit-testing ngdescribe

我有一个指令,在我的输入上添加验证器以供ng-messages使用:

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-describe):

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();
        });
    }
});

但是有两个问题(可能有相同的原因??):

  1. 第一次测试失败,因为$scope.model.dateundefined(应该使用'01/01/2001'进行更新,因为它是有效输入)。我注意到来自$modelValue的{​​{1}}也未定义。我知道form.dateField已更新,因为如果它不胜,那就是$scope.model.date。但为什么null
  2. 第二次测试失败,因为undefined停留$valid(因为它无效,所以应该使用true进行更新)。我再次问:为什么?
  3. 我尝试将我的代码与this post answer进行比较,但我不知道我的错误在哪里。

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

    我正在使用Angular 1.4.7,如果它有帮助..

    编辑:

    尝试使用相同的结果this methodfalse保持未定义,而$modeValue$viewValue使用正确的字符串进行更新。

    这些是我测试的日志:

    $$rawModelValue

1 个答案:

答案 0 :(得分:0)

我的错:我传递的是String而不是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);

发现在仅检查一个条目时使用“test”方法更有效。

对于那些花时间的人抱歉。