我正在使用jest和jasmine-marbles来测试我的ngrx-effects。到目前为止还不错,但是我有一个特殊的情况,我需要使用withLatestFrom在如下的效果中访问Store:
@Effect()
createDataSourceSuccess$ = this.actions$
.ofType<sourceActions.CreateDataSourceSuccess>(
sourceActions.DataSourceActionTypes.CreateDataSourceSuccess
)
.pipe(
map(action => action.dataSource),
withLatestFrom(this.store.select(getSourceUploadProgress)),
switchMap(([source, progress]: [DataSource, UploadProgress]) =>
of(
new sourceActions.StartSourceUploadProgress({
id: source.fileId,
uploadProgress: progress,
})
)
)
);我也像这样设置了我的测试:
it('should return StartSourceUploadProgress for CreateDataSourceSuccess', () => {
const dataSource = dataSources[0];
const action = new dataSourceActions.CreateDataSourceSuccess(dataSource);
const outcome = new dataSourceActions.StartSourceUploadProgress({
id: dataSource.fileId,
uploadProgress: null,
});
store.select = jest.fn(_selector => of(null));
actions.stream = hot('-a-', { a: action });
const expected = cold('--b', { b: outcome });
expect(effects.createDataSourceSuccess$).toBeObservable(expected);
});我还注意到,我成功地为Store和Actions设置了模拟,因为所有其他测试都工作得很好。与其他效果之间的唯一区别是,这些效果中不存在Store和withLatestFrom。
最后,这是我得到的错误输出:
DataSourceEffects › should return StartSourceUploadProgress for CreateDataSourceSuccess
TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.有什么想法吗?
发布于 2018-08-31 20:46:23
这个解决方案对我很有效
it('should return StartSourceUploadProgress for CreateDataSourceSuccess', () => {
const dataSource = dataSources[0];
const action = new dataSourceActions.CreateDataSourceSuccess(dataSource);
const outcome = new dataSourceActions.StartSourceUploadProgress({
id: dataSource.fileId,
uploadProgress: null,
});
actions.stream = hot('-a-', { a: action });
const expected = cold('--b', { b: outcome });
store.select = jest.fn().mockImplementationOnce(() => of(new SourceUploadProgress()));
expect(effects.createDataSourceSuccess$).toBeObservable(expected);
});https://stackoverflow.com/questions/52114492
复制相似问题