用笑话和茉莉花大理石测试ngrx效果时的问题

时间:2018-08-31 11:27:40

标签: angular ngrx ngrx-effects jasmine-marbles

我正在使用jestjasmine-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);
});

我还要注意,我成功为StoreActions设置了模拟,因为其他所有测试都可以正常工作。两者之间的唯一区别是在这些效果中没有StorewithLatestFrom

最后这是我得到的错误输出:

 DataSourceEffects › should return StartSourceUploadProgress for CreateDataSourceSuccess

    TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

有什么想法吗?

1 个答案:

答案 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);
});
相关问题