使用业力的角度单元测试,测试分支或serviceSpy()错误

时间:2020-01-10 14:18:15

标签: angular unit-testing karma-jasmine karma-runner

我目前正在Angular 7中测试一个角度应用程序,但我不知道如何测试Submit函数的此分支,在此组件中大约有20多个测试,其中一些甚至使用相同的服务间谍作为无法正常工作的测试:

分支:

submit(): void {

    if (this.systemTable) {
      // this.formArray = this.formBuilder.array([]);
      while (this.formArray.length) {
        this.formArray.removeAt(0);
        console.log('dfdfdf');
      } //this.formArray.removeAt(0);


    } //...

我的测试:

it('submit() should remove when systemTable is true', () => {
    app.ngOnInit();

    app.systemTable = true; //Entering the branch i wan't to test

    const f = app.formArray; 
    app.addRow(); //This function adds a row into the app.formRow, I checked it's values with a debugger and it's working
    const userServiceSpy = spyOn(app.formArray, 'removeAt');

    app.submit();
    expect(userServiceSpy).toHaveBeenCalledTimes(1);
  });

1 个答案:

答案 0 :(得分:0)

将循环从while更改为for,在角度8中将不再需要该循环。