【问题标题】:How to properly test a revert for 'onlyOwner' modifier?如何正确测试“onlyOwner”修饰符的恢复?
【发布时间】:2022-01-20 18:45:47
【问题描述】:

在继承自 OpenZeppelin 的 OwnableAccessControl 的 Solidity 合约中,我有一个函数,合约所有者可以调用该函数来为帐户提供 ADMIN 角色。它看起来像这样:

function addAdmin(address account) public virtual onlyOwner {
   _grantRole(ADMIN, account);
}

在我的测试文件中,我使用 OpenZeppelin 的 test-environmenttest-helper 与 Mocha 和 Chai。我正在尝试测试从非所有者帐户调用addAdmin() 的失败。我的测试如下所示:

it('denies access to non-Owners to add a new ADMIN', async function() {
   await expectRevert(
      await pool.addAdmin(notlisted1, { from: notlisted2 }),
      "Ownable: caller is not the owner"
   );
});

似乎addAdmin() 正确还原,但测试失败。此测试失败时我在命令行看到的消息是:

Error: Returned error: VM Exception while processing transaction: revert Ownable: caller is not the owner -- Reason given: Ownable: caller is not the owner.

对这里发生的事情有什么想法吗?为什么expectRevert() 断言没有通过?

【问题讨论】:

    标签: solidity truffle openzeppelin


    【解决方案1】:

    这实际上是一个简单的错误。在pool.addAdmin() 之前使用await 是不正确的。删除它解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 2015-03-08
      • 2018-04-06
      • 2022-07-09
      • 2016-07-09
      • 2022-11-18
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      相关资源
      最近更新 更多