这是我需要测试的文件。我正在使用Angular5 (^5.2.0)、ngrx5 (^5.2.0),目前我的注意力集中在一些效果服务上。这是我目前需要测试的代码。但我不知道如何正确地实现它,我做了一些尝试,但它们只是失败。你们有小费吗?谢谢
import { Injectable } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
import { FETCH_DATA } from '../reducers/data.reducer';
import { DataService } from './data.service';
import { Subject } from 'rxjs/Subject';
import { ActionWithPayload } from '../types/app-actions';
@Injectable()
export class AutoCompleteEffects {
public autoComplete$ = new Subject<{
type: string;
payload: { results: string[]; searchValue: string };
}>();
@Effect()
getData$ = this.actions$.ofType(FETCH_DATA).switchMap((action: ActionWithPayload) => {
return this.data
.getData(action.payload)
.map(results => ({
type: 'FETCHED_DATA',
payload: { results, searchValue: action.payload }
}))
.catch(() =>
Observable.of({
type: 'FETCHED_DATA',
payload: { results: [], searchValue: action.payload }
})
);
});
constructor(
private actions$: Actions,
private data: DataService,
) {
this.getData$.subscribe(action => this.autoComplete$.next(action));
}
public getAutoCompleteEffects() {
return this.autoComplete$.asObservable();
}
}发布于 2018-06-13 18:04:32
有几件事你需要拥有
在那之后,这只是一个不同动作的测试
it('should suggest auto complete values', () => {
actions = of([<test-actions>]);
const expectedEffects = of([<expected-actions-from-effects>]);
expect(TestBed.get(AutoCompleteEffects).getData$).toBeObservable(expectedEffects);
});https://stackoverflow.com/questions/50841046
复制相似问题