1

我正在为 ngrx 效果编写单元测试。我尝试了所有方法,但似乎无法克服此错误:

Expected $[0].notification.kind = 'E' to equal 'N'.
Expected $[0].notification.value = undefined to equal Object({ vendors: [ Object({ id: 1234, code: 'tst', name: 'test', freePlay: true, freeSpin: false, disabled: true, active: false }) ], type: '[CasinoVendors API] Get Vendors Success' }).
Expected $[0].notification.error = TypeError: Cannot read property 'sort' of undefined to equal undefined.
Expected $[0].notification.hasValue = false to equal true.

我要测试的效果是:

getVendors$ = createEffect(() => this.actions$.pipe(
    ofType(CasinoVendorsActions.getVendors),
    concatMap(() => this.casinoVendorsSvc.getVendors()
      .pipe(
        map((data: any) => {
          let vendors: VendorModel[] = data

          return CasinoVendorsActions.getVendorsSuccess({vendors: vendors});
        })
      ))
    )
  );

单元测试:

describe('getVendors', () => {
    it('should return a stream with getVendorsSuccess action', () => {
      const vendors: VendorModel[] = [{
        id: 1234,
        code: 'tst',
        name: 'test',
        freePlay: true,
        freeSpin: false,
        disabled: true,
        active: false,
      }];
      const action = CasinoVendorsActions.getVendors();
      const outcome = CasinoVendorsActions.getVendorsSuccess({vendors: vendors});

      actions$ = hot('-a', { a: action });
      const response = cold('-a|', { a: vendors });

      casinoVendorsSvc.getVendors.and.returnValue(response);

      const expected = cold('--b', { b: outcome });

      expect(effects.getVendors$).toBeObservable(expected);
    });
  });

由于效果返回的操作“getVendorsSuccess”填充了正确的数据,我怀疑问题出在这部分:

const expected = cold('--b', { b: outcome });

expect(effects.getVendors$).toBeObservable(expected);

任何建议将不胜感激

4

1 回答 1

0

您将供应商理解为 let vendors: VendorModel[] = data.payload;

但是您的服务返回const vendors: VendorModel[] = [...];

您可能必须将其包装在一个对象中{data: (vendors: VendorModel[])}

于 2019-09-12T11:32:34.460 回答