我创建了一个获取用户确认的模式,并基于用户确认触发了一些可以正常工作的动作。对于上述情况,我已编写了适用的规范。 但是当我检查代码覆盖率时,模态中的某些代码未被覆盖。我可以在单元测试中覆盖以下代码吗?
规格:
describe('#collectReason', () => {
it('open reason modal when save to cloud', () => {
bsModalService.show = (): BsModalRef => {
return { hide: null, content: { reason: new EventEmitter() }, setClass: null };
};
component.collectReason();
expect(component.modalService.show).toBeTruthy();
});
it('display modal message when reason modal is opened', () => {
bsModalService.show = (): BsModalRef => {
return { hide: null, content: { reason: new EventEmitter() }, setClass: null };
};
const modalRef = { hide: null, content: { reason: new EventEmitter() }, setClass: null };
const modalMessage = {
title: 'Enter Reason',
};
spyOn(bsModalService, 'show').and.returnValue(modalRef);
component.collectReason();
expect(bsModalService.show).toHaveBeenCalledWith(ModalReasonComponent, {
initialState: { ...modalMessage },
});
});
});