我有一个带有两个指令的directives.js文件,我想申请单元测试,以提高覆盖率,但我不知道该怎么做
这是directive.js文件:
(function () {
'use strict';
angular
.module('app')
.directive('numbersOnly', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
var transformedInput = inputValue ? inputValue.replace(/[^\d.-]/g,'') : null;
if (transformedInput!=inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
}
})
.directive('numbersOnlyPositive', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
var transformedInput = inputValue ? inputValue.replace(/[^\d.]/g,'') : null;
if (transformedInput!=inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
}
});
})();
这是测试文件(directives.spec.js):
(function () {
'use strict';
describe('Unit testing directives general', function() {
var $compile,
$rootScope,
element,
vm;
// Load the app module, which contains the directive
beforeEach(module('app'));
// Store references to $rootScope and $compile
// so they are available to all tests in this describe block
beforeEach(inject(function(_$compile_, _$rootScope_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$compile = _$compile_;
$rootScope = _$rootScope_;
vm.mesHastaSelected = "3";
vm.cantidadHastaSelected = "5";
//element = angular.element('<input text ng-model="mesHastaSelected">');
}));
it('should accept number input only', function() {
expect(vm.cantidadHastaSelected).toBe('1');
});
it('should accept number positive input only', function() {
expect(vm.mesHastaSelected).toBe('8');
});
});
})();
这是html代码输入字段:
<input type="text" ng-model="vm.mesHastaSelected" maxlength="2" numbers-only-positive />
<input type="text" ng-model="vm.cantidadHastaSelected" maxlength="4" numbers-only />
感谢您的帮助,