在我使用@Output() 发出后,我尝试测试我的 toHaveBeenCalled 函数"@angular/core": "~12.2.0"
,"ngx-bootstrap": "^7.1.0"
这是我的代码
主要组件:
showModal() {
this.modalRef = this.modalService.show(MyModalComponent);
this.modalRef.content.emitBack.subscribe((response: Boolean) => {
if (response) {
this.mySimpleFunction();
}
});
}
模态组件:
@Output() emitBack = new EventEmitter();
constructor(private modalService: BsModalService) { }
ngOnInit() {
}
closeModal() {
this.modalService.hide();
this.emitBack.emit(true);
}
我想期待mySimpleFunction();
被叫
,但我得到了TypeError: Cannot read properties of undefined (reading 'content')
这是我的单元测试:
describe('showModal', () => {
let component: MyMainComponent;
let modalService: BsModalService;
beforeEach(() => {
modalService = jasmine.createSpyObj('BsModalService', ['hide', 'show']);
component = new DocumentComponent(modalService);
});
it('should call function after modal close', () => {
component.modalRef = modalService.show(MyModalComponent);
spyOn(component.modalRef.content, 'emitBack');
component.showModal();
expect(component.mySimpleFunction).toHaveBeenCalled();
});
});