angular - 如果一个动作已在另一个动作中分派,如何进行单元测试?
问题描述
正如标题所说,我想测试我的操作在可观察返回时调度另一个操作。
这是我的代码:
@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)
推荐阅读
- python - 烧瓶:会话不工作。显示会话对象没有“已修改”属性
- html - Styled-components:最小宽度为 768px 的媒体查询以 300px 宽度应用
- java - Android Java:将应用程序创建的文件保存到下载文件夹
- django - Django:使用 __lte 自动发布帖子
- latex - 表格中的新命令
- google-sheets - 如何在 Google 表格中按司机计算平均每小时吨数?
- excel - 基于上个月的 Excel 公式
- javascript - 在 JavaScript 中使用“new”关键字创建包装的 BigInt 或 Symbol 有区别吗?
- javascript - html中的内容可编辑段落
- python - 如何创建一个简单的 python web 服务,它将在本地运行并处理并行请求