2017-07-25 64 views
0

下面我写下了代码中有一个异步函数,它在内部使用setTimeout()并在3秒后打印消息。如何在ionic2中使用karma/Jasmine测试异步函数?

我必须用Jasmine编写规范。

我跟着docs,但我没有得到如何在代码中应用此。

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. 
}) 
}); 

在标测试规范的意见,我需要编写规范。

回答