代码之家  ›  专栏  ›  技术社区  ›  fastcodejava

在角度测试中被调用但不显示的功能

  •  0
  • fastcodejava  · 技术社区  · 7 年前

    我有这个密码。

    export class MyComponent implements OnInit {
    
       ngOnInit() {
          // some code 
          this.method1(); // It comes up to here.
       }
    
       method1() {
           console.log('method1'); // It is not showing this.
           // Other method calls
       }
    }
    

      it('should call method1', fakeAsync(() => {
        const element = fixture.debugElement.nativeElement;
        spyOn(comp, 'method1');
    
    
        fixture.detectChanges();
        // some code 
        fixture.detectChanges();
    
    
        fixture.whenStable().then(async() => {
           expect(comp.method1).toHaveBeenCalled();  // this works
        });
      }));
    

    这里可能出了什么问题?

    1 回复  |  直到 7 年前
        1
  •  2
  •   kaosdev    7 年前

    当你监视一个方法时,你正在用一个假的方法替换原来的方法,这就是为什么没有调用method1

    spyOn(comp, 'method').and.callThrough()