首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS自定义验证指令单元测试:模型值未定义

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

Stack Overflow用户
提问于 2016-05-17 17:21:10
回答 1查看 497关注 0票数 1

我有一个指令,在输入中添加一个验证器,以供ng消息使用:

代码语言:javascript
复制
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描述):

代码语言:javascript
复制
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'更新,因为它是一个有效的输入)。我注意到来自form.dateFieldform.dateField也没有定义。我知道$scope.model.date被更新了,因为如果它不更新的话,它就是null。但为什么是undefined
  2. 第二个测试失败了,因为$valid保持为true (它应该用false更新,因为它是无效的)。我再问一遍:为什么?

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

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

我的工作角度是1.4.7,如果有帮助的话..。

编辑:

用同样的结果尝试了this method$modeValue始终未定义,而$viewValue$$rawModelValue则使用正确的字符串进行更新。

这些是我测试的日志:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-22 13:27:52

我的错:我传递了一个字符串而不是一个RegExp.

我还将返回声明替换为以下内容:

代码语言:javascript
复制
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“方法更有效。

对不起那些花时间在上面的人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37282651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档