【问题标题】:what does 1x 3x etc mean in karma code coverage report in Angular Unit testing?Angular 单元测试中业力代码覆盖率报告中的 1x 3x 等是什么意思?
【发布时间】:2017-06-05 02:21:35
【问题描述】:

我是 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 等的重要性。请帮助我了解它的重要性。

【问题讨论】:

    标签: javascript unit-testing angular istanbul karma-coverage


    【解决方案1】:

    它表示该行已执行的次数。

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

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

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

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

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

    【讨论】:

    • 感谢@echonax 的回答。如果您忘记告诉我更多信息,请与我分享,或者您可以在答案中详细说明。再次非常感谢您。接受你的回答。
    • @IsettyRavitejakumar 我已经更新了我的答案,因为你已经更新了你的问题。但它没有显着的重要性。也许如果一条线被执行了很多次,你可能想检查一下这条线是否被不必要地使用了,但我非常怀疑这会是一个案例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    相关资源
    最近更新 更多