2017-03-11 101 views
0
PlayOscillator(hz: number = 400, durationInSeconds: number = 1){ 
    this._Oscillators[0].frequency.value = hz; 
    this._Oscillators[0].connect(this._MainGainNode); 
    this._activeOscillatorIndex = 0; 

setTimeout(() => { 
    console.info("time out called"); 
    this._Oscillators[this._activeOscillatorIndex].disconnect(this._MainGainNode); 
this._Oscillators[this._activeOscillatorIndex] = undefined; 
}, durationInSeconds * 1000); 
} 

我想测试typeofthis._Oscillators[0]执行setTimeout后,但我无法做到这一点。我已经在这里找到了所有解决方案推荐功能不可用茉莉花2.5如何测试在茉莉花2.5中触发setTimeout的函数?

我迄今

 it('Should switch from one oscillator to another',()=>{ 
       let wap = webAudioApiProvider; 
      expect(wap._Oscillators[0]).toBeUndefined(); 
      expect(()=>{wap.PlayOscillator(freq,duration)}).not.toThrow(); 
      expect(wap._Oscillators[0]).toBeUndefined(); 

     }); 
+0

到目前为止,您的测试代码在哪里? – JLRishe

+0

对不起,我以为我贴了它。当我从杂货店回来时,我会在10分钟内编辑我的问题。它只是一个三行代码,期望不会丢失。 ToBe – distante

+0

@JLRishe更新! – distante

回答

0

您应该能够使用done参数,因为茉莉花2.0支持:

it('Should switch from one oscillator to another', done => { 
    let wap = webAudioApiProvider; 
    expect(wap._Oscillators[0]).toBeUndefined(); 
    expect(()=>{wap.PlayOscillator(freq,duration)}).not.toThrow(); 

    setTimeout(() => { 
     expect(wap._Oscillators[0]).toBeUndefined(); 
     done(); 
    }, duration * 1000 + 100); // add 100 to ensure this fires after the wap's timeout 
}); 
+0

感谢它的工作,但我需要忽略来自tslint的错误,说“无法找到名称完成”,也许茉莉花的类型不是最新的。 – distante