Во-первых, я новичок в модульном тестировании или тестировании в целом, так что я все еще пытаюсь понять некоторые концепции. У меня уже есть базовая c модульного тестирования, и я также в настоящее время изучаю rx js мраморное тестирование, поэтому в настоящее время я застрял на том, как тестировать поиск w / c, функционирует так же, как и код в документации.
Итак, давайте воспользуемся кодом из обзора героев в angular документации
this.heroes$ = this.searchTerms.pipe(
// wait 300ms after each keystroke before considering the term
debounceTime(300),
// ignore new term if same as previous term
distinctUntilChanged(),
// switch to new search observable each time the term changes
switchMap((term: string) => this.heroService.searchHeroes(term)),
);
Вот мой текущий тест выглядит как
const searchResults$ = (inputs$: Observable<string>) =>
inputs$.pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap((term: string) => fakeHeroService.searchHeroes(term))
);
it('should return the correct results', () => {
scheduler.run(helpers => {
const { cold, hot, expectObservable } = helpers;
// these are the search terms typed by user, observable
// emitted by this.searchTerms
const inputMarbles = '^-a 300ms b-c 300ms';
// each emitted response by each service call
const outputMarbles = '-- 300ms a-- 300ms c';
// verify that service is called N times
// verify that it's passed with certain argument per run
searchServiceSpy = spyOn(heroService, 'searchHeroes').and.returnValue(
cold(outputMarbles)
);
const source$ = hot(inputMarbles);
const output$ = source$.pipe(searchResults$);
expectObservable(output$).toBe(outputMarbles);
/* expect(fakeSearchService.getMatches).toHaveBeenCalledTimes(2); */
});
});
Я просто могу не получается.