下面我写下了一个异步函数的代码,它在内部使用 setTimeout(( 并在 3 秒后打印消息。
我必须使用茉莉花编写规范。
我已经遵循了文档,但我不知道如何在代码中应用它。
首页
//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
}
}
家.规格
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.
})
});
在测试规范中标记的注释中,我需要编写规范。
使用 done(( 方法。看看这个。
http://www.htmlgoodies.com/html5/javascript/using-the-done-method-in-your-jasmine-driven-asynchronous-javascript-tests.html