Solidity
如何在安全帽測試中驗證功能
實際上我正在驗證一個函式來檢查只有白名單成員或所有者可以訪問這個函式但是當我執行這個測試文件時我得到恢復錯誤那個 錯誤
Validations should revert error for non whitelisted memeber: Error: VM Exception while processing transaction: reverted with reason string 'Only owner or whiteListed members allowed' at KeyboardNFT.isWhiteListedorOwner (contracts/nft_erc721.sol:1102)
程式碼
describe("Validations", function () { it("should revert error for non whitelisted memeber", async function () { const {hardhatToken, owner, addr1, addr2} = await loadFixture(deployTokenFixture); expect(await hardhatToken.connect(addr1).awardItem(addr1.address,"first nft")).to.throw("Only owner or whiteListed members allowed"); }); });
改變
.to.throw ...
至
.to.be.revertedWith(<revertMessage>)
例子 :
await expect(Contract.add()).to.be.revertedWith("Only owner or whiteListed members allowed");