Jasmine Jamine中的done方法只是等待几秒钟,或者也对其他对象执行它
我写了这个规范,我发布了一个事件。我等待事件被接收和处理。要等待,我只需使用Jasmine Jamine中的done方法只是等待几秒钟,或者也对其他对象执行它,jasmine,Jasmine,我写了这个规范,我发布了一个事件。我等待事件被接收和处理。要等待,我只需使用done。这让我想知道,done只是等待几秒钟,还是还做了其他事情 fit('should show dialog box when uploading a file is aborted', (done) => { let newComponent = component; console.log("component is ",newComponent); let file1 = ne
done
。这让我想知道,done
只是等待几秒钟,还是还做了其他事情
fit('should show dialog box when uploading a file is aborted', (done) => {
let newComponent = component;
console.log("component is ",newComponent);
let file1 = new File(["dd"], "file1",{type:'image/png'});
spyOn(newComponent,'showDialog');
let reader = newPracticeQuestionComponent.handleFileSelect([file1]);
expect(reader).toBeTruthy();
expect(reader.onabort).toBeTruthy();
reader.abort();
expect(newPracticeQuestionComponent.showDialog).toHaveBeenCalled();
/*done is nothing but wait it seems. It makes the test case wait. This is required as the event
fired (error) is async. So taht the test case doesn't fiinish before the event is handled, done/wait
is added.
*/
done();
});
done
是等待,但不是我想的那样。它不是始终运行的超时。我认为done
在Jasmine
中充当了一个检查点。当Jasmine
看到某个规范使用了done
时,它知道除非运行了包含done
的代码段,否则无法继续下一步(比如运行下一个规范或将此规范标记为finished)
例如,jasmine通过了这个规范,尽管它应该失败,因为它没有等待setTimeout被调用
fit('lets check done',()=>{
let i=0;
setTimeout(function(){
console.log("in timeout");
expect(i).toBeTruthy();//the spec should fail as i is 0
},1000);
//jasmine reaches this point and see there is no expectation so it passes the spec
});
但如果我的意图是让Jasmine在setTimeout
中等待异步代码,那么我在异步代码中使用done
fit('lets check done',(done)=>{
let i=0;
setTimeout(function(){
console.log("in timeout");
expect(i).toBeTruthy();//with done, the spec now correctly fails with reason Expected 0 to be truthy.
done();//this should make jasmine wait for this code leg to be called before startinng the next spec or declaring the verdict of this spec
},1000);
});
注意,在我想要检查断言的地方应该调用done
fit('lets check done',(done)=>{
let i=0;
setTimeout(function(){
console.log("in timeout");
expect(i).toBeTruthy();//done not used at the right place, so spec will incorrectly ypass again!.
//done should have been called here as I am asserting in this code leg.
},1000);
done();//using done here is not right as this code leg will be hit inn normal execution of it.
});