[英]Function is never called inside a switchMap with unit test angular + jasmine and SpyOn
我試圖做一個測試,以了解是否調用了服務的功能,但總是返回不調用我。 我不知道我在做什么錯。 我想知道的功能是否在switchMap中被稱為此功能,該功能是一項服務。 (this.heroSearchService.search(term))
這是顯示Expected spy search to have been called.
的消息Expected spy search to have been called.
這是我的組件。
export class HeroSearchComponent implements OnInit {
heroes: Observable<Hero[]>;
private searchTerms = new Subject<string>();
constructor(
private heroSearchService: HeroSearchService
) {}
search(term: string): void {
// Push a search term into the observable stream.
this.searchTerms.next(term);
}
ngOnInit(): void {
this.heroes = this.searchTerms.pipe(
debounceTime(300), // wait for 300ms pause in events
distinctUntilChanged(), // ignore if next search term is same as previous
switchMap(term => term ? this.heroSearchService.search(term) : of<Hero[]>([])),
catchError(error => {
// TODO: real error handling
console.log(`Error in component ... ${error}`);
return of<Hero[]>([]);
})
);
}
}
這是我的模擬服務。
export const MockHeroSearchService = {
search: (term: string): Observable<Array<Hero>> => {
let heroes = defaultHeroes.filter(hero => hero.name.includes(term)) as Array<Hero>;
return of(heroes);
}
}
這是我的測試文件。 在此文件中,此測試是我在其中創建間諜的測試( spyOn(heroSearchService, 'search').and.callThrough();
),失敗的expect(heroSearchService.search).toHaveBeenCalled();
是expect(heroSearchService.search).toHaveBeenCalled();
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [CommonModule, HttpClientTestingModule, RouterTestingModule, FormsModule], //If our component uses routing, httpclient
declarations: [HeroSearchComponent], //Here we put all the components that use our component.
providers: [
//Here we can inject the dependencies that our component needs.
//If our dependecies are services, we can create a simulated service.
{ provide: HeroSearchService, useValue: MockHeroSearchService },
]
}).compileComponents();
}));
//Arrange
beforeEach(() => {
heroSearchService = TestBed.get(HeroSearchService);
fixture = TestBed.createComponent(HeroSearchComponent);
debugElement = fixture.debugElement;
heroSearchComponent = fixture.componentInstance;
//fixture.detectChanges();// Comments, so that it does run the of method ngOnInit();
});
fit('should with the correct search term, the variable heros have at least a hero', fakeAsync(() => {
spyOn(heroSearchService, 'search').and.callThrough();
fixture.detectChanges();
const input = fixture.debugElement.query(By.css('#search-box'));
input.nativeElement.value = 'And';
input.triggerEventHandler('keyup', null);
tick(600);
fixture.detectChanges();
expect(heroSearchService.search).toHaveBeenCalled();
}));
我也運行了代碼覆蓋率,這就是結果。 在哪里顯示該行正在執行。
我希望你能幫助我。 請原諒我的英語。
當我遇到相同/相似的問題時,恢復了這個相當古老的威脅。 您有能力找到解決方案嗎? 就我而言-由於記錄了中間值-我可以驗證ids $ Observable是否正在觸發。
this.testObs$ = this.ids$.pipe(
switchMap(ids => {
const promises = ids.map(id => {
return this.testApi.getPayer(id).toPromise();
});
return Promise.all(promises);
})
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.