【发布时间】:2021-06-12 14:17:06
【问题描述】:
我想为下面的函数写一个单元测试用例:
onfactorblur():void{
var that=this;
setTimeout(function(){
var target=document.activeElement;
if(target.tagName=="BUTTON"){
if(target.textContent=="CONTINUE"){
that.onContinueClick();
}
else{
that.activeModal.close();
}
}
},.5);
}
我是角度单元测试的新手,从来没有遇到过我必须在函数中测试 setTimeout 的情况。我试过了:
it('Should close the modal if target element is button',fakeAsync(()=>{
spyOn(component,'onContinueClick');
spyOn(component.activeModal,'close');
tick(1);
fixture.detectChanges();
fixture.whenStable().then(()=>{
//What should I write here
})
}));
【问题讨论】:
-
你应该提取setTimeOut中的函数并单独测试。
-
@KrishnaMohan 谢谢...但我已经测试过 onContinueClick 和 activeModal.close() 。你能证明你的建议吗?
标签: javascript angular unit-testing karma-jasmine angular-test