2016-09-28 22 views
2

我必須做一些錯誤的解決,但是這是我的測試用例:青鳥承諾變成在任何情況下

const { describe, it } = require('mocha'), 
    should = require('should'), 
    Promise = require('bluebird') //v3.4.6 
describe('Bluebird',() => { 
    it('Promise is never resolved but does it get resolved?',() => { 
    new Promise(() => false) 
     .should.be.fulfilled() // It really shouldn't be 
    }) 
}) 

通過,但它不應該失敗?

+1

爲什麼你有雙重斷言?處理「拒絕」的承諾是一個決心。 –

+0

@ DanielA.White'.rejectedWith'將會*通過*,但我注意到這個承諾也被解析**,這就是這個測試用例所展現的。這裏沒有鏈接承諾 –

+0

我會看看摩卡如何看待承諾的狀態。 –

回答

1

在使用摩卡測試中的承諾時,從測試中對return the promise非常重要。

在你的情況,這將是:

it('Promise is never resolved but does it get resolved?',() => { 
    return new Promise(() => false) 
    .should.be.fulfilled() 
}) 

然而,這可能是還沒有正是你需要的在這裏,作爲承諾的履行不能被調用should的時間來確定。您的實際測試可能不同,最重要的部分仍然是退還承諾鏈。

當你這樣做的時候,你不需要進一步斷言承諾的履行/拒絕,因爲這是摩卡隱含的做法。

我個人很喜歡chai-as-promised,它可以讓你使用和以前完全相同的測試,但這一次,它會工作。

+0

謝謝。 '應該'確實可以處理像'承諾'這樣的異步性' –

+0

@PeteV。啊,我不知道。偉大的:) –