我想多次监视一项服务。为此,我使用了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();
})
是否有避免这种重复的方法?
为什么这样做,在组件public
中使该服务constructor
,然后将间谍用作component.cacheService