3 回答

TA貢獻(xiàn)1784條經(jīng)驗 獲得超8個贊
這是我的看法:
運用 async/await
不需要額外的柴模塊
避免捕獲問題,@ TheCrazyProgrammer在上面指出
延遲的promise函數(shù),如果延遲為0則失?。?/p>
const timeoutPromise = (time) => {
return new Promise((resolve, reject) => {
if (time === 0)
reject({ 'message': 'invalid time 0' })
setTimeout(() => resolve('done', time))
})
}
// ↓ ↓ ↓
it('promise selftest', async () => {
// positive test
let r = await timeoutPromise(500)
assert.equal(r, 'done')
// negative test
try {
await timeoutPromise(0)
// a failing assert here is a bad idea, since it would lead into the catch clause…
} catch (err) {
// optional, check for specific error (or error.type, error. message to contain …)
assert.deepEqual(err, { 'message': 'invalid time 0' })
return // this is important
}
assert.isOk(false, 'timeOut must throw')
log('last')
})
積極的測試相當(dāng)簡單。意外故障(模擬500→0)會自動失敗,因為被拒絕的承諾會升級。
否定測試使用try-catch-idea。但是:'抱怨'不希望的傳遞只發(fā)生在catch子句之后(這樣,它不會在catch()子句中結(jié)束,觸發(fā)進(jìn)一步但誤導(dǎo)性的錯誤。
要使此策略起作用,必須從catch子句返回測試。如果你不想測試其他任何東西,請使用另一個() - 阻止。
- 3 回答
- 0 關(guān)注
- 846 瀏覽
添加回答
舉報