如何在ionic2中使用karma / Jasmine测试异步函数? [英] How to test an asynchronous function using karma/Jasmine in ionic2?
本文介绍了如何在ionic2中使用karma / Jasmine测试异步函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面我写下了有异步函数的代码,它在内部使用setTimeout()并在3秒后打印消息。
Below I have written down the code in which there is an asynchronous function, that uses setTimeout() internally and prints message after 3seconds.
我必须写使用Jasmine的规范。
I have to write the spec using Jasmine.
我已经关注 docs 但我没有得到如何在代码中应用此功能。
I have followed the docs but I didn't get how to apply this in the code.
home.ts
//implementing only function written in a class.All imports are done properly.
click(){
function delayedAlert(message: string, time: number, cb) {
return setTimeout(() => cb(message), time);
}
delayedAlert('Aditya3', 3000, (message) => console.log(message));//function is called
}
}
home.spec.ts
describe('Asynchronous call', () => {
let fixture: ComponentFixture<HomePage>;
let comp: HomePage;
let de: DebugElement;
let el: HTMLElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [HomePage],
imports: [
IonicModule.forRoot(HomePage),
],
providers: [NavController],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(HomePage);
comp = fixture.componentInstance;
de = fixture.debugElement
});
it('Asynchronous testing with callback', ()=>{
//how to write the spec here for the asynchronous function wrriten in the above class using Karma/Jasmine.
})
});
在测试规范中标注的评论中,我需要编写规范。
In the comments marked in the test spec, I need to write the spec.
推荐答案
使用done()方法。看一下这个。
Use the done() method. Check this out.
这篇关于如何在ionic2中使用karma / Jasmine测试异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文