5 Mart 2013, Salı
Özel doğrulama açısal yönergesi test için
Bu özel doğrulama Direktifi örnek resmi açısal sitesinde sunulmaktadır. http://docs.angularjs.org/guide/forms Giriş sayı biçiminde bir metin denetler.
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;
}
});
}
};
});
Birim test için bu kodu ben yazdım:
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);
});
});
});
});
O zaman bu hata alıyorum:
Expected undefined to equal 5.
Error: Expected undefined to equal 5.
Baskı tablolar her yerde neler olup bittiğini görmek için koydum, emir, asla denir gibi görünüyor. Böyle basit bir yönerge test etmek için uygun bir yolu nedir?
CEVAP
6 Temmuz 2013, CUMARTESİ
Dışarı açısal-app kod https://github.com/angular-app/angular-appokuyarak anladım Bu video da 10* *çok yardımcı olur
Yaptığım iki hata:
- Doğrudan ng-model yaparken kapsamında bağlayın
- Form denetleyici direktifler geçmesi için ne doğrudan işlemek için kullanın
İşte güncelleştirilmiş sürümünü. Direktif aynı, değişen tek testtir.
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);
});
});
});
Bunu Paylaş:
Nasıl Açısal js bir form için özel doğ...
Nasıl özel yöntemleri, alanları veya i...
Özel bir yöntem kamu test birimi için....
'birim test korumalı için en iyi ...
Rspec, Raylar: nasıl denetleyicileri ö...