的問題,這種做法是(await fails()).should.throw(Error)
沒有意義。
await
解決了Promise
。如果Promise
拒絕,則拋出拒絕值。
所以(await fails()).should.throw(Error)
永遠不會工作:如果fails()
拒絕,錯誤拋出,並且.should.throw(Error)
永遠不會執行。
您擁有的最習慣的選擇是使用Chai的rejectedWith
屬性,如您在問題中所示。
下面是一個簡單的例子。與你在你的問題中所展示的沒什麼不同。我只是使用async
函數的wins()
和fails()
和expect
而不是should
。當然,您可以使用返回Promise
和chai.should
的函數。
const chai = require('chai')
const expect = chai.expect
chai.use(require('chai-as-promised'))
// Always succeeds
async function wins() {
return 'Winner'
}
// Always fails with an error
async function fails() {
throw new Error('Contrived Error')
}
it('wins() returns Winner', async() => {
expect(await wins()).to.equal('Winner')
})
it('fails() throws Error', async() => {
await expect(fails()).to.be.rejectedWith(Error)
})
如果你喜歡要你的wins()
測試,使其更接近您的fails()
測試,你可以寫你的wins()
測試,如下所示:
it('wins() returns Winner', async() => {
await expect(wins()).to.eventually.equal('Winner')
})
最關鍵的事情中的任何一種,這些例子記是chai-as-promised
對其功能返回承諾,如rejectedWith
和eventually.something
。因此,你必須在一個async
測試功能的情況下await
他們,否則失敗的情況仍然會通過:
async function wins() {
return 'Loser'
}
async function fails() {
return 'Winner'
}
it('wins() returns Winner', async() => {
expect(wins()).to.eventually.equal('Winner')
})
it('fails() throws Error', async() => {
expect(fails()).to.be.rejectedWith(Error)
})
如果運行上面的代碼測試,你會得到如下:
$ npm test
> [email protected] test /home/vsimonian/code/mocha-chai-async
> mocha .
√ wins() returns Winner
(node:13836) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rej
ection id: 1): AssertionError: expected 'Loser' to equal 'Winner'
(node:13836) [DEP0018] DeprecationWarning: Unhandled promise rejections are dep
recated. In the future, promise rejections that are not handled will terminate
the Node.js process with a non-zero exit code.
√ fails() throws Error
(node:13836) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rej
ection id: 2): AssertionError: expected promise to be rejected with 'Error' but
it was fulfilled with 'Winner'
2 passing (11ms)
正如你所看到的,柴的斷言實際上失敗了,但是他們在一個承諾的背景下失敗了,即沒有人編輯過await
版或catch
版。因此,Mocha認爲沒有任何失敗,並將測試標記爲已通過,但Node.js(如上所述將在未來改變的行爲)將未處理的拒絕打印到終端。
嘗試在函數中封裝'await fails()'來在你的測試中調用它,並在這個函數上應用'should.throw' – Troopers
我試過了,但是await必須封裝在一個異步函數中 - 它本身那麼需要等待。 D'哦! – plexer