2

我有一个返回动作 A 然后是动作 B 的效果

@Effect() myEffect$: Observable <Action> = this.actions$
  .ofType('MY_ACTION')
  .switchMap(() => Observable.of(
    // subscribers will be notified
    { type: 'ACTION_ONE' },
    // subscribers will be notified (again ...)
    { type: 'ACTION_TWO' }
  ));

如何测试两个连续返回的操作?

it('should return action one then action two', () => {
  runner.queue(new myAction());
  const expectedResult = twoSuccesiveActions;
  sessionEffect.myEffect$.subscribe(result => {
    // how do I test those two succesively returned actions
    expect(result).toEqual(expectedResult);
  });
});
4

4 回答 4

4

您可以使用一个 withtake(1)和一个 with skip(1)

it('should return action one then action two', () => {
  const expectedResult = twoSuccesiveActions;
  sessionEffect.myEffect$.take(1).subscribe(result => {
    // first action
    expect(result).toEqual(expectedResult);
  });

  sessionEffect.myEffect$.skip(1).take(1).subscribe(result => {
    // second action
    expect(result).toEqual(expectedResult);
  });

  runner.queue(new myAction());
});

无论如何,take(1)如果您不手动取消订阅以确保不会泄漏到其他测试等中,我建议您使用...

于 2017-03-10T17:07:17.713 回答
1

如果有人仍然想知道如何做,这是另一种方法

effects.myEffect$
  .pipe(
    bufferCount(2)
  )
  .subscribe((emittedActions) => {
    /* You could also include here callings to services
        verify(myServiceMock.execute(anything()))
          .called();
    */
    expect(emittedActions.map((action) => action.type))
      .toEqual([
        myFirstAction,
        mySecondAction,
      ]);
    done();
  });
于 2019-07-22T16:39:30.400 回答
1

像这样成对使用:

it('should return a ACTION_ONE && ACTION_TWO',
  inject([EffectService, EffectsRunner], (service: EffectService, runner: EffectsRunner) => {
    runner.queue({type: USER_SWITCH_ROLE});

    service.myEffect$
      .pairwise()
      .subscribe(([result1, result2]) => {
        expect(result1).toEqual({type: ACTION_ONE, payload: {}});
        expect(result2).toEqual({type: ACTION_TWO, payload: {}});
      });
  }));
于 2017-04-19T02:50:43.190 回答
0

我发现了toArray运营商:

“收集所有源排放,并在源完成时将它们作为数组发射。”

sessionEffect.myEffect$
  .pipe(toArray())
  .subscribe(result =>
    expect(result).toHaveLength(2);
    expect(result[0]).toBeInstanceOf(ExpectedAction1);
    expect(result[1]).toBeInstanceOf(ExpectedAction2);
  });
于 2021-10-13T01:31:25.387 回答