2016-12-29 45 views
1

我試圖測試引發錯誤的發電機功能如下內,用玩笑和柴:測試發生器內部拋出錯誤?

// function 

    function * gen() { 
     yield put({ type: TIME_OUT_LOGOUT }) 
     throw new Error('User has logged out') 
    } 

    //test 

    let genFunc = gen() 

    expect(genFunc().next).to.deep.equal(put({ type: TIME_OUT_LOGOUT })) 

    expect(genFunc().next).to.throw(Error('User has logged out')); 

但它無法正常工作。這將是測試這個的正確方法?

回答

1

試着改變你的測試代碼genFunc().nextgenFunc.next().value


編輯:

的說法應該是:

expect(genFunc.next().value).toEqual(put({type: TIME_OUT_LOGOUT})); expect(genFunc.next).toThrow(Error);

對於第二斷言expect(() => genFunc.next()).toThrow(Error);也將工作,但包裝r函數() => genFunc.next()是不必要的,因爲genFunc.next不帶任何參數。

+0

它似乎沒有工作,這實際上使敏感,因爲發電機功能沒有其他next.value,因爲沒有其他產量剩下。 – Nicolas

+1

你的第一個斷言是否工作?無論如何,我已經做了一個修正。下面是我的工作(使用茉莉花語法):'expect(genFunc.next()。value).toEqual(put({type:TIME_OUT_LOGOUT})); ()=> genFunc.next())。toThrow(Error('User has logged out'));'注意第二個斷言中'genFunc.next()'附近的包裝函數。 – Assan

+0

好吧,我得到它的工作,changin斷言: expect(genFunc.next().value).toEqual(put({type:TIME_OUT_LOGOUT})); expect(()=> genFunc.next())。toThrow(Error); 如果你想發佈你的答案,所以我可以將其標記爲解決方案。非常感謝! – Nicolas

相關問題