这种自定义的验证指令是在官方网站的角度提出了一个例子。 http://docs.angularjs.org/guide/forms它检查文本输入是数字格式或没有。
var INTEGER_REGEXP = /^\-?\d*$/;
app.directive('integer', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
if (INTEGER_REGEXP.test(viewValue)) {
// it is valid
ctrl.$setValidity('integer', true);
return viewValue;
} else {
// it is invalid, return undefined (no model update)
ctrl.$setValidity('integer', false);
return undefined;
}
});
}
};
});
单元测试这个代码,我写了这个:
describe('directives', function() {
beforeEach(module('exampleDirective'));
describe('integer', function() {
it('should validate an integer', function() {
inject(function($compile, $rootScope) {
var element = angular.element(
'<form name="form">' +
'<input ng-model="someNum" name="someNum" integer>' +
'</form>'
);
$compile(element)($rootScope);
$rootScope.$digest();
element.find('input').val(5);
expect($rootScope.someNum).toEqual(5);
});
});
});
});
然后,我得到这个错误:
Expected undefined to equal 5.
Error: Expected undefined to equal 5.
我把打印语句随处可见,看看是怎么回事,它看起来像指令永远不会被调用。 什么是测试一个简单的指令,这样的正确方法?
对方回答的测试应该写成:
describe('directives', function() {
var $scope, form;
beforeEach(module('exampleDirective'));
beforeEach(inject(function($compile, $rootScope) {
$scope = $rootScope;
var element = angular.element(
'<form name="form">' +
'<input ng-model="model.somenum" name="somenum" integer />' +
'</form>'
);
$scope.model = { somenum: null }
$compile(element)($scope);
form = $scope.form;
}));
describe('integer', function() {
it('should pass with integer', function() {
form.somenum.$setViewValue('3');
$scope.$digest();
expect($scope.model.somenum).toEqual('3');
expect(form.somenum.$valid).toBe(true);
});
it('should not pass with string', function() {
form.somenum.$setViewValue('a');
$scope.$digest();
expect($scope.model.somenum).toBeUndefined();
expect(form.somenum.$valid).toBe(false);
});
});
});
需要注意的是$scope.$digest()
现在被调用后$setViewValue
。 这台形式为“脏”的状态,否则将保持“原始”,这可能是不是你想要的。
我想通了阅读角应用代码https://github.com/angular-app/angular-app此视频还可以帮助过http://youtu.be/ZhfUv0spHCY?t=31m17s
两个错误,我提出:
- 当你正在做的NG-模型不直接绑定到该范围
- 使用形式的控制器直接操作该怎么传递的指令
这里是更新的版本。 该指令是,我改变了一样,只有测试。
describe('directives', function() {
var $scope, form;
beforeEach(module('exampleDirective'));
beforeEach(inject(function($compile, $rootScope) {
$scope = $rootScope;
var element = angular.element(
'<form name="form">' +
'<input ng-model="model.somenum" name="somenum" integer />' +
'</form>'
);
$scope.model = { somenum: null }
$compile(element)($scope);
$scope.$digest();
form = $scope.form;
}));
describe('integer', function() {
it('should pass with integer', function() {
form.somenum.$setViewValue('3');
expect($scope.model.somenum).toEqual('3');
expect(form.somenum.$valid).toBe(true);
});
it('should not pass with string', function() {
form.somenum.$setViewValue('a');
expect($scope.model.somenum).toBeUndefined();
expect(form.somenum.$valid).toBe(false);
});
});
});
测试我的自定义指令在对象物搜索“$错误”的自定义验证的名称。 例:
'use strict';
describe('Directive: validadorCorreo', function () {
// load the directive's module
beforeEach(module('sistemaRegistroProCivilApp'));
var inputCorreo, formulario, elementoFormulario, scope, $compile;
beforeEach(inject(function ($rootScope, _$compile_) {
scope = $rootScope.$new();
$compile = _$compile_;
elementoFormulario = angular.element('<form name="formulario">' +
'<input type="text" name="correo" data-ng-model="correo" required data-validador-correo/>' +
'</form');
scope.correo = '';
elementoFormulario = $compile(elementoFormulario)(scope);
scope.$digest();
inputCorreo = elementoFormulario.find('input');
formulario = scope.formulario;
console.log(formulario.correo.$error);
}));
it('Deberia Validar si un correo ingresado en el input es correcto e incorrecto', inject(function ($compile) {
inputCorreo.val('eric+@eric.com').triggerHandler('input');
expect(formulario.correo.$error.email).toBe(true); //Here, the name of the custom validation appears in the $error object.
console.log(formulario.correo.$error);
inputCorreo.val('eric@eric.com').triggerHandler('input');
expect(formulario.correo.$error.email).toBeUndefined();//Here, the name of the custom validation disappears in the $error object. Is Undefined
console.log(formulario.correo.$error.email)
}));
});
我希望我可以帮助你!