代码之家  ›  专栏  ›  技术社区  ›  Szabolcs

使用jest和jasmine大理石测试ngrx效果时出现问题

  •  1
  • Szabolcs  · 技术社区  · 8 年前

    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);
    });
    

    我还注意到我成功地为 商场 为了 Actions 因为其他的测试都很好。这个和其他的唯一区别是 商场 和 最晚从

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

     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 回复  |  直到 8 年前
        1
  •  2
  •   Kliment Ru    8 年前

    这个解决方案对我有用

    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);
    });
    
    推荐文章