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'));
但它無法正常工作。這將是測試這個的正確方法?
它似乎沒有工作,這實際上使敏感,因爲發電機功能沒有其他next.value,因爲沒有其他產量剩下。 – Nicolas
你的第一個斷言是否工作?無論如何,我已經做了一個修正。下面是我的工作(使用茉莉花語法):'expect(genFunc.next()。value).toEqual(put({type:TIME_OUT_LOGOUT})); ()=> genFunc.next())。toThrow(Error('User has logged out'));'注意第二個斷言中'genFunc.next()'附近的包裝函數。 – Assan
好吧,我得到它的工作,changin斷言: expect(genFunc.next().value).toEqual(put({type:TIME_OUT_LOGOUT})); expect(()=> genFunc.next())。toThrow(Error); 如果你想發佈你的答案,所以我可以將其標記爲解決方案。非常感謝! – Nicolas