angular - 如何将内部功能测试为角度功能?
问题描述
我有一个问题要问你们。是任何方式来测试内部函数,我讲successCallback和failureCallback;因为当我检查代码覆盖率时,这些功能没有被覆盖。
onSubmitForm() {
const name: string = this.newForm.controls.name.value;
const successCallback = () => {
this.popupService.popupSuccess();
};
const failureCallback = () => {
this.popupService.popupFailure();
};
this.myservice.edit({ name, this.id }, successCallback, failureCallback);
}
解决方案
我认为这样的事情可以奏效。
it('should call popupSuccess on success', () => {
component.onSubmitForm();
// get the 2nd argument
const successCallback = mockMyService.edit.calls.mostRecent().args[1];
successCallback();
expect(mockPopupService.popupSuccess).toHaveBeenCalled();
});
it('should call popupFailure on failure', () => {
component.onSubmitForm();
// get the 3rd argument
const failCallback = mockMyService.edit.calls.mostRecent().args[2];
failCallback();
expect(mockPopupService.popupFailure).toHaveBeenCalled();
});
推荐阅读
- android - 将测试应用程序 ID 与 adMob 一起使用时出现无效应用程序 ID 错误
- css - 为什么这个 CSS 代码在我的内盒中溢出,即使它适合我的外盒?
- node.js - 从 mongodb 的嵌套文档数组中删除一条记录
- python - odoo 10 打印 XLSX 报告时访问被拒绝
- python - 使用 PyArrow 时如何捕获 Python UDF 异常
- oracle - 我们什么时候使用 WITH 子句,它的主要好处是什么?
- php - 如何在 php 中读取 Neo4j 响应数组
- node.js - 如何让 VSCode 附加到由 NPM 启动的 nodemon?
- angular - 不能使用 formGroup.controls['xxx'].setValue('someString');
- file - 是否可以通过删除文件夹的名称来编写一个简单的代码来批量重命名文件夹内的文件?