代码之家  ›  专栏  ›  技术社区  ›  Mansoor Ahmed

Angular 4 Jasmine单元测试指令导出函数

  •  0
  • Mansoor Ahmed  · 技术社区  · 7 年前

    我在angular 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”的单元测试。有谁能就如何实现出口功能覆盖提出建议吗。

    1 回复  |  直到 7 年前
        1
  •  1
  •   user4676340 user4676340    7 年前

    覆盖范围很简单

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

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

    推荐文章