Angular 单元测试中的业力代码覆盖率报告中的 1x 3x 等是什么意思?

what does 1x 3x etc mean in karma code coverage report in Angular Unit testing?

我是 Angular 中的单元测试新手。我获得了带有代码覆盖率的 karma 设置以及 angular-cli 。我有 运行 命令 ng-test 并打开了代码覆盖率报告。我在该覆盖率报告中看到 1x3x 等以及我的代码行号。请找到我的报道报告图片。

这是我的测试用例代码app.component.spec.ts

/* tslint:disable:no-unused-variable */

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    });
  });

  it('should create the app', async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'app works!'`, async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app works!');
  }));

  it('should render title in a h1 tag', async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    let compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('app works!');
  }));
});

我不明白 1x,2x,3x 等在我的代码报告中的重要性。请帮助我了解它的重要性。

表示该行被执行的次数。

根据您的代码让我们看一下您的 title 字段:

首先执行:expect(app).toBeTruthy();

第二个:expect(app.title).toEqual('app works!');

第三名:expect(compiled.querySelector('h1').textContent).toContain('app works!');

这就是为什么它在左侧显示 3x。