如果一个动作已在另一个动作中分派,如何进行单元测试?

How to unit test if an action has been dispatched in another action?

正如标题所说,我想测试当可观察 returns.

时我的动作是否调度另一个动作

这是我的代码:

@Action(FetchRoles)
fetchRoles(ctx: StateContext<RoleStateModel>, action: FetchRoles) {
  return this.rolesGateway.fetchRoles()
    .pipe(
      tap(roles => ctx.patchState({ roles })),
    );
}

@Action(UpdateMyRole)
updateMyRole(ctx: StateContext<RoleStateModel>, action: UpdateMyRole) {
  return this.rolesGateway.updateMyRole(action.roleToUpdate)
    .pipe(
      tap(() => {
        ctx.dispatch(new FetchRoles()); // <-- WHAT I WANT TO SPY AND TEST
        this.feedbackManagerService.success('Role updated');
      }),
    );
}

我试过同步测试,像这样

store.dispatch(new UpdateApplicationRole(applicationCode, roleManage));

expect(gateway.updateMyRole).toHaveBeenCalledTimes(1);
expect(gateway.updateMyRole).toHaveBeenCalledWith(roleToUpdate);
expect(gateway.fetchRoles).toHaveBeenCalledTimes(1);
expect(gateway.fetchRoles).toHaveBeenCalledWith();
expect(feedbackManagerService.success).toHaveBeenCalledTimes(1);
expect(feedbackManagerService.success).toHaveBeenCalledWith('Role updated');

而且还有fakeAsync ... tick(100)

store.dispatch(new UpdateApplicationRole(applicationCode, roleManage));
tick(100)
...

甚至 async ... await

await store.dispatch(new UpdateApplicationRole(applicationCode, roleManage));
...

但每次,我都只在 gateway.updateMyRole 接到电话,之后的 4 天都没有。 最初,我只想测试 ctx.dispatch(new FetchRoles())

感谢您的帮助

我想多了。我只需要用一个值来模拟我的网关可观察对象: of() => of(null)