假设我想为这份合同样本做一个测试:
// assume we import OpenZeppelin's Ownable
contract Metaverse extends Ownable, ... {
...
// Assume this is just to experiment and learn.
// Then I can question myself how useful is to
// actually do this, and why.
function renounceOwnership() public override onlyOwner {
revert("I explicitly disabled this method");
}
...
}而且,使用块菌石,我希望对该方法何时恢复进行测试:
const Metaverse = artifacts.require("Metaverse");
contract("Metaverse", function (accounts) {
const deployer = accounts[0];
const newOwner = accounts[1];
it("respects the safe-ownership appropriately", async function () {
let metaverse = await Metaverse.deployed();
try
{
metaverse.renounceOwnership({from: deployer});
assert.fail("Ownership should not be rejected");
}
catch(e) { /* Ok :) - It will be an AssertionError */ }
});
});到目前为止,这是可行的,但我想让它变得更干净。所以我把我的问题分成两部分:
发布于 2022-01-18 14:16:10
您可以在revertedWith柴Matcher:https://ethereum-waffle.readthedocs.io/en/latest/matchers.html#revert-with-message中使用Waffle
在这里可以看到一个例子:https://github.com/gnosis/safe-contracts/blob/main/test/libraries/MultiSend.spec.ts#L51
https://ethereum.stackexchange.com/questions/119243
复制相似问题