angular - 如何测试 rxjs 链中的函数
问题描述
我正在尝试测试回调在关闭后发生mat-dialog
。我的代码显示为 rxjs 链,我需要测试该链中的一些函数
这是带有 Angular 材质的 Angular 7
测试代码
deleteProject() {
this.dialog.open(DeleteProjectDialogComponent, {
data: {
project: this.project
},
minWidth: MIN_DIALOG_WIDTH
})
.afterClosed()
.pipe(
take(1),
filter(Boolean),
switchMap(() => this.apiProjects.deleteProject(this.project.id)), // <= need to test this call of this function
withLatestFrom(this.translate.get('PROJECT.WAS_DELETED')),
tap({
next: ([res, translation]) => {
this.utils.storage.clearProject();
this.utils.navigation.goToAuthPage();
this.snackBar.open(translation, 'OK', {
duration: SNACK_BAR_DURATION
});
},
error: res => {
const {error} = res;
if (res.status === 400) {
const {name, project} = error;
if (project) {
this.showError(error.project);
}
}
}
})
).subscribe();
}
我的测试
it('should call api after delete project', () => {
const spyApi = spyOn(apiProject, 'deleteProject');
spyOn(dialog, 'open').and.returnValue({
afterClosed: () => of(true)
});
component.deleteProject();
expect(spyApi).toHaveBeenCalled(); // <= failed with Error: Expected spy deleteProject to have been called.
});
解决方案
推荐阅读
- c# - 通过DCOM从C#中的C++中检索字符串数组
- python - Blob input binding with Azure Function in Python is not working (inputblob.name is None"). How to troubleshoot?
- r - R:如何重载 + 运算符来调用将函数添加到 R6 类的方法?
- mysql - Sql查询查找具有最大里程的汽车
- javascript - 点击destroy with cocoon后,如何在不销毁记录的情况下重新计算发票
- arrays - 我可以在 MongoDB 文档中修改数组的值,但它不会在数据库中更新
- javascript - 从未定义的数组值映射的反应按钮
- uiviewcontroller - iOS 13 PageSheet 父 UIViewController 不会变黑
- javascript - HTML字符串格式化函数算法的问题
- r - 有没有人可以使用 R 解释基本正则表达式的结果?