首页 > 解决方案 > 如何避免对默认“应该创建”方法的 api 调用

问题描述

当我使用 Angular cli 创建组件时,ng-cli 会为我生成一些默认测试。但是这个测试在我的詹金斯身上间歇性地失败了。

以下是更多细节:

例子.component.ts

ngOnit(){
   this.get1stData();
   this.get2ndData();
}

get1stData(){
  this.service.get1stData().subscribe()....
}

get2ndData(){
 this.service.get2ndData().subscribe()....
}

example.component.spec.ts

  beforeEach(() => {
    fixture = TestBed.createComponent(NamespaceHomeComponent);
    component = fixture.componentInstance;
    service = TestBed.get(service);
    fixture.detectChanges();
  });
  it('should create', () => {
    expect(component).toBeTruthy();
  });

如果 api 有问题,这个简单的测试会失败。我怎样才能避免在这里调用 api。

我收到此错误:

  {
    "message": "An error was thrown in afterAll\nUncaught [object Object] thrown",
    "str": "An error was thrown in afterAll\nUncaught [object Object] thrown"
  }

标签: angularunit-testingjasmine

解决方案


推荐阅读