我如何正确地用摩卡咖啡和柴测试诺言?

我如何正确地用摩卡咖啡和柴测试诺言?,第1张

我如何正确地用摩卡咖啡和柴测试诺言?

最简单的方法是使用Mocha在最新版本中提供的内置Promise支持:

it('Should return the exchange rates for btc_ltc', function() { // no done    var pair = 'btc_ltc';    // note the return    return shapeshift.getRate(pair).then(function(data){        expect(data.pair).to.equal(pair);        expect(data.rate).to.have.length(400);    });// no catch, it'll figure it out since the promise is rejected});

或者使用现代Node和async / await:

it('Should return the exchange rates for btc_ltc', async () => { // no done    const pair = 'btc_ltc';    const data = await shapeshift.getRate(pair);    expect(data.pair).to.equal(pair);    expect(data.rate).to.have.length(400);});

因为这种方法是端到端的承诺,所以它更易于测试,您不必考虑正在考虑的奇怪情况,就像

done()
到处都是奇怪的电话一样。

这是Mocha目前与Jasmine等其他库相比所具有的优势。您可能还需要检查Chai As
Promised
,这将使其变得更加容易(否

.then
),但就我个人而言,我更喜欢当前版本的清晰度简洁性



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4979476.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存