首页 > 解决方案 > 如果一个动作已在另一个动作中分派,如何进行单元测试?

问题描述

正如标题所说,我想测试我的操作在可观察返回时调度另一个操作。

这是我的代码:

@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())

谢谢您的帮助

标签: angularrxjsjestjsngxs

解决方案


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


推荐阅读