你能幫我解釋一下如何編寫測試來捕捉錯誤嗎?捕捉承諾錯誤的測試
例如,我有這個假代碼:
export default function someFunc (num) {
return someOtherFunc(num).then(resp => {
return resp + 1
}).catch(() => {
return 0
})
}
這是以前的代碼玩笑測試。這是成功測試:
test('Should return 3',() => {
expect.assertions(1)
return expect(someFunc(2)).resolves.toBe(3)
})
我想測試捕捉錯誤,我該怎麼做呢?本次測試將失敗,錯誤「預計一個斷言被調用,但只收到零個說法叫」:
test('Should return 3',() => {
expect.assertions(1)
return expect(someFunc()).rejects.toBe(0)
})
很抱歉,如果這個問題是愚蠢的。我只是學習承諾