使用spyon时的代码覆盖率问题

时间:2017-07-04 07:26:05

标签: angular typescript karma-runner karma-jasmine istanbul

我正在使用unit testcomponent(Angular2应用)撰写Karma-Jasmine。我正在使用Istanbul代码覆盖率报告。

这是我的测试用例,

it('Should Invoke onNext function', async(() => {
    const fixture = TestBed.createComponent(LoginComponent);
    fixture.detectChanges();
    const login = fixture.componentInstance;

    spyOn(login, 'onNext');

    let email = fixture.debugElement.nativeElement.querySelector("input[name='username']");
    email.value = "email";

    let nextButton = fixture.debugElement.nativeElement.querySelectorAll("button")[1];
    nextButton.click();

    fixture.whenStable().then(() => {
      expect(login.onNext).toHaveBeenCalled();
    })
  }));

正如您所见,我spying on onNext function验证是否在nextbutton click上调用了它。它工作正常,测试通过。

但是我的登录页面的代码覆盖率报告显示未涵盖onNext功能。 enter image description here

我做错了什么?

如果我不监视onNext函数,该函数也会被覆盖,

it('Should Invoke onNext function', async(() => {
    const fixture = TestBed.createComponent(LoginComponent);
    fixture.detectChanges();
    const login = fixture.componentInstance;


    let email = fixture.debugElement.nativeElement.querySelector("input[name='username']");
    email.value = "email";

    let nextButton = fixture.debugElement.nativeElement.querySelectorAll("button")[1];
    nextButton.click();
  }));

enter image description here

1 个答案:

答案 0 :(得分:8)

使用此:

spyOn(login, 'onNext').and.callThrough()

相关问题