我有打开模态以批准删除项目的代码:
handleDeleteMarkingPeriod(deleteModel: DeleteModel) {
this.openModalToAcceptDelete('Are you sure you want to delete this item?', deleteModel);
}
openModalToAcceptDelete(message = '', deleteModel: DeleteModel) {
this.deleteModalReference = this.modalService.open(DeleteApprovalModalComponent, {backdrop: 'static'});
this.deleteModalReference.componentInstance.message = message;
this.deleteModalReference.result.then((result) => {
if (result.approve) {
this.deleteMarkingPeriod(deleteModel);
}
}, (reason) => {
console.log(reason);
});
}
当this.deleteModalReference.result.then
被触发时,我正在检查响应,如果result.approve
是true
,则我正在采取措施。
问题是:如何从值this.deleteModalReference.result.then
的.spec文件{approve: true}
许诺中触发,我的测试用例如下:
it('it should open modal and accept delete', () => {
component.schoolCalendars = schoolCalendars;
spyOn(component, 'deleteMarkingPeriod');
component.handleDeleteMarkingPeriod({
calendarId: '7',
markingPeriodId: '18'
});
expect(modalService.open).toHaveBeenCalled();
expect(component.deleteMarkingPeriod).toHaveBeenCalled();
});
测试响应为:
FAILED TESTS:
ManageCalendarTableComponent
markingPeriodForm
delete
✖ it should open modal and accept delete
Chrome 77.0.3865 (Mac OS X 10.14.5)
Expected spy deleteMarkingPeriod to have been called.
一切正常,我只是不知道如何在.then()内部运行代码,这是对服务本身的嘲笑:
modalService = TestBed.get(NgbModal);
modalRef = modalService.open(DeleteApprovalModalComponent);
spyOn(modalService, 'open').and.returnValue(modalRef);