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

前端之家收集整理的这篇文章主要介绍了angular – 使用spyon时的代码覆盖率问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Karma-Jasmine为我的组件(Angular2应用程序)编写单元测试.我正在利用伊斯坦布尔的代码覆盖率报告.

这是我的测试用例,

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

正如你所看到的那样,我正在监视onNext函数,以验证是否在nextbutton click上调用它.它工作正常,测试通过.

但是我的“登录页面代码覆盖率报告显示未涵盖onNext函数.

enter image description here

我究竟做错了什么??

如果我不监视onNext函数,该函数也会被覆盖,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

解决方法

用这个:

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

猜你在找的Angularjs相关文章