我正在使用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.
有什么想法吗?
答案 0 :(得分:2)
此解决方案对我有用
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);
});