Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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函数.

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

我究竟做错了什么??

如果我不监视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(); }));

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

解决方法

用这个

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

大佬总结

以上是大佬教程为你收集整理的angular – 使用spyon时的代码覆盖率问题全部内容,希望文章能够帮你解决angular – 使用spyon时的代码覆盖率问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。