我正在使用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.
有什么想法吗?
这个解决方案适合我
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);
});