checkDefault(event) {
try {
this.defaultCard = event.checked;
this.cardInfo.forEach((s) => {
if (s.isDefault && event.checked) {
const dialog = this.dialogService.dialogMethod(this.message.isDefaultAlertCard, this.dialogType.confirmation, true);
dialog.afterClosed().subscribe((res) => {
Eif (!res) {
this.defaultCard = false;
}
});
}
});
} catch (err) {
this.errorObject.message = 'Error in checkDefault: ' + err && err.message ? err.message : null;
this.sharedService.errorEmailNotification(this.storeId, this.errorObject);
this.dialogService.dialogMethod(this.message.failedSave, this.dialogType.failure,
true
);
}
}
在这个函数中,我覆盖了 try 块,但没有覆盖 catch 块。如何覆盖它。
我的 try 块测试用例是
test('should create checkDefault', () => {
component.cardInfo = [{ isDefault: true, customer: "CustomerId", cardId: "cardId" }, { isDefault: false, customer: "CustomerId", cardId: "cardId" }];
component.checkDefault({ checked: true });
expect(component.checkDefault).toBeTruthy();
});
我找到了答案
component.checkDefault(null);
监视
dialogMethod
并抛出错误,这将执行 catch 块。
spyOn(dialogService, 'dialogMethod').and.callFake(() => { throw new Error('test error message') })