第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用mocha和chai正確測試承諾?

如何用mocha和chai正確測試承諾?

拉莫斯之舞 2019-09-20 16:48:26
以下測試表現(xiàn)得很奇怪:it('Should return the exchange rates for btc_ltc', function(done) {    var pair = 'btc_ltc';    shapeshift.getRate(pair)        .then(function(data){            expect(data.pair).to.equal(pair);            expect(data.rate).to.have.length(400);            done();        })        .catch(function(err){            //this should really be `.catch` for a failed request, but            //instead it looks like chai is picking this up when a test fails            done(err);        })});我該如何妥善處理被拒絕的承諾(并對其進(jìn)行測試)?我該如何正確處理失敗的測試(即:expect(data.rate).to.have.length(400);?這是我正在測試的實現(xiàn):var requestp = require('request-promise');var shapeshift = module.exports = {};var url = 'http://shapeshift.io';shapeshift.getRate = function(pair){    return requestp({        url: url + '/rate/' + pair,        json: true    });};
查看完整描述

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子句返回測試。如果你不想測試其他任何東西,請使用另一個() - 阻止。


查看完整回答
反對 回復(fù) 2019-09-20
  • 3 回答
  • 0 關(guān)注
  • 846 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號