加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:23:21 所属栏目:安全 来源:网络整理
导读:我正在使用Karma-Jasmine为我的组件(Angular2应用程序)编写单元测试.我正在利用伊斯坦布尔的代码覆盖率报告. 这是我的测试用例, it('Should Invoke onNext function',async(() = { const fixture = TestBed.createComponent(LoginComponent); fixture.detect
我正在使用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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读