如何正确测试'onlyOwner'修饰符的还原?



在继承自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()断言不通过?

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

最新更新