角4茉莉花单元测试指令导出功能



我在角度 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"的单元测试。任何人都可以建议如何实现出口功能的覆盖率。

覆盖很容易

it('should cover the function', () => {
  appValidator(new FormControl(''));
});

如果这是你问的。但是,您还应该测试您的函数是否按预期工作。

最新更新