简体   繁体   English

使用 Karma 进行 Angular 单元测试,测试分支或 serviceSpy () 出错

[英]Angular Unit Tests using Karma, error with test branch or serviceSpy ()

I am currently testing an angular application in Angular 7, but I can't figure out how to test this branch of the submit function, I have approximately more 20 tests in this component, and some of them even use the same service spy as the test that isn't working:我目前正在 Angular 7 中测试一个 angular 应用程序,但我不知道如何测试提交函数的这个分支,我在这个组件中有大约 20 多个测试,其中一些甚至使用相同的服务间谍作为测试不起作用:

The branch:分支:

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);


    } //...

My test:我的测试:

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);
  });

将循环从 while 更改为 for,在 angular 8 中将不再需要此循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM