我在角度4中创建了一个自定义指令,该指令在下面。
import { Directive } from '@angular/core';
import { NG_VALIDATORS, FormControl } from '@angular/forms';
export function appValidator(control: FormControl) {
let tWordsLength = false;
if (control.value) {
let test = control.value.split(",");
var lengths = test.map(function (word) {
if(word.length > 30){
tWordsLength = true;
}
return word.length
})
if (test.length > 10 || tWordsLength) {
return {
tagsDomain: {
parsedDomain: false
}
}
}
}
return null;
}
@Directive({
selector: '[appGeneral][ngModel]',
providers: [
{
provide: NG_VALIDATORS,
useValue: appValidator,
multi: true
}
]
})
export class GeneralDirective {
};
我的规格如下
import {TestBed} from '@angular/core/testing';
import { GeneralDirective } from './app-general.directive';
describe('GeneralDirective', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [GeneralDirective]
});
});
it('should create an instance', () => {
const directive = new GeneralDirective();
expect(directive).toBeTruthy();
});
});
我想在指令中介绍导出功能'appValidator'的单元测试。任何人都可以建议如何实现出口功能的覆盖。
答案 0 :(得分:1)
覆盖范围很容易
it('should cover the function', () => {
appValidator(new FormControl(''));
});
如果这是你问的话。但是您还应该测试您的功能是否按预期工作。