2
我已經在TypeScript中使用摩卡測試框架和chai斷言庫(我對這三者都是新手)寫了一些測試,並且看到拋出了一個錯誤,並在下面的代碼中調用了assert.fail()。但是,測試仍然被標記爲通過。我很困惑,爲什麼會發生這種情況,以及我是否在承諾方面做錯了什麼。另外,如下所示,還有一個UnhandledPromiseRejectionWarning。我不明白爲什麼這被標記爲未處理,因爲我已經包含了catch塊。我將不勝感激任何關於如何解決此問題的建議,謝謝!異步摩卡測試(與Chai斷言庫)應該失敗,但被標記爲傳遞
User.all()
.then((users) => {
expect(users.length).to.equal(0);
return User.create('[email protected]', '12345', 'test', true, true);
})
.then(() => {
return User.all();
})
.then((users) => {
expect(users.length).to.equal(1);
})
.catch((error) => {
assert.fail();
});
調試控制檯
✓ should create a new record
with existing e-mail in the database
(node:29903) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): AssertionError: assert.fail()
(node:29903) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
在'Users.all'之前加一個'return' –
謝謝!哈哈我只是想出來,即將發佈,你擊敗了我。我正在閱讀這篇文章(https://alisdair.mcdiarmid.org/simple-nodejs-tests-with-assert-and-mocha/),並閱讀「爲此編寫測試,請確保您的代碼塊返回一個諾言,摩卡將會照顧其他人:「 – Lauren