我正在嘗試使用 js 在安全帽中為我的智能合約撰寫一個測驗,我想檢查一些東西以防我的合約呼叫失敗問題是當“失敗的合約呼叫”行運行時它會恢復整個塊測驗并且不會運行其余部分。我怎樣才能讓它作業?
some code 1
await contract.function()
// contract call fails intentionally
some code 2
我需要“一些代碼 2”的結果,但我只是收到一個錯誤,即呼叫失敗。
uj5u.com熱心網友回復:
假設您使用的是 Hardhat Chai Matchers 擴展,您可以使用expect()
to.be.revertedto.be.revertedWith()to.be.revertedWithPanic()to.be.revertedWithCustomError()to.be.revertedWithoutReason()
例子:
it("fails to call the function", async () => {
await expect(
contract.function()
).to.be.revertedWith("Custom revert message");
});
檔案:https ://hardhat.org/hardhat-c??hai-matchers/docs/overview#reverts
uj5u.com熱心網友回復:
這是測驗的標準要求,只需使用chai而不是創建自己的測驗引擎。
https://www.chaijs.com/api/bdd/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529459.html
上一篇:如何在測驗環境中初始化存盤庫?
下一篇:關于Cstrlen函式的問題
