為什么用最終表示的測驗通過?我顯然在語法或用例中遺漏了一些東西。
const chai = require('chai')
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const {expect} = chai
function p() {
return new Promise( (resolve) => {
setTimeout( () => {
resolve(10);
},5000);
})
}
describe('Chai Eventually test', () => {
it('should fail!', () => {
expect(p()).to.eventually.equal(5)
});
it('should also fail!', async () => {
expect(await p()).to.equal(5)
});
})
運行它mocha我得到:
Chai Eventually test
? should fail!
1) should also fail!
1 passing (2s)
1 failing
1) Chai Eventually test
should also fail!:
AssertionError: expected 10 to equal 5
expected - actual
-10
5
at Context.<anonymous> (chaiError.js:19:26)
1 passing (4ms)
這會立即發生,因此它不會等待承諾解決。
uj5u.com熱心網友回復:
你有幾個選擇。
return最簡單的就是expect:
it('should fail!', () => {
return expect(p()).to.eventually.equal(5); // <-- return this expectation
});
或者,您可以跳過return并使用done傳遞給 chai.notify方法的回呼:
it('should fail using done!', (done) => { // <-- pass done into spec function
expect(p()).to.eventually.equal(5).notify(done); // <-- pass done to .notify()
});
檔案在這里。
這是一個 StackBlitz顯示它在這些更改下正常作業(即失敗)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483070.html
標籤:javascript 节点.js 单元测试 柴
上一篇:有沒有辦法匹配UITableViewDiffableDataSource中的節數匹配字典鍵計數
下一篇:如何為字典C#創建單元測驗
