2017-07-25 11 views
0

以下は、内部的にsetTimeout()を使用し、3秒後にメッセージを出力する非同期関数があるコードを書いたものです。ionic2でカルマ/ジャスミンを使って非同期関数をテストする方法は?

ジャスミンを使用して仕様を記述する必要があります。

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

、私は仕様を記述する必要があります。

答えて

0

done()メソッドを使用します。これをチェックしてください。彼らはasync.itなど使用されているこの記事jasmine.async libに、では

http://www.htmlgoodies.com/html5/javascript/using-the-done-method-in-your-jasmine-driven-asynchronous-javascript-tests.html

+0

、あなたは私がjasmine.asyncのlibを使用せずに、この仕様を書くために助けることができますか? – Aditya

+0

私はそれを実装することができません。あなたが助けますか? – Aditya

関連する問題