Angular-茉莉花-重置并重新加载组件

问题描述 投票:0回答:1

我想多次监视一项服务。为此,我使用了jasmine.getEnv().allowRespy(true);语句。

[当我这样做时,我发现必须重新创建组件为了进行更改。我这样做:

fixture = TestBed.createComponent(CardDisplayPageComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();

我最终得到这样的代码:

 it('should have a datasource when cache has data', () => {
    spyOn(cacheService, 'getFromCache').and.returnValue([CARD_GENERIC_DATA_MOCK]);

    fixture = TestBed.createComponent(CardDisplayPageComponent);
    component = fixture.debugElement.componentInstance;
    fixture.detectChanges();

    expect(component.dataSource).toBeTruthy();
  })


  it('should have a datasource when cache does not have data', () => {
    spyOn(cacheService, 'getFromCache').and.returnValue([undefined]);
    spyOn(seasonsFetchingService, 'getTransformedData').and.returnValue(of(CARD_PAGE_GENERIC_DATA_MOCK));

    fixture = TestBed.createComponent(CardDisplayPageComponent);
    component = fixture.debugElement.componentInstance;
    fixture.detectChanges();

    expect(component.dataSource).toBeTruthy();
  })

是否有避免这种重复的方法?

angular typescript unit-testing jasmine karma-jasmine
1个回答
0
投票

为什么这样做,在组件public中使该服务constructor,然后将间谍用作component.cacheService

© www.soinside.com 2019 - 2024. All rights reserved.