Hardhat测试 – 即使与合约抛出的错误匹配,revertedWithCustomError也失败了。

huangapple go评论46阅读模式
英文:

Hardhat testing - revertedWithCustomError fails even though it matches the error thrown from contract

问题

I am trying to write some unit tests for a Solidity contract. I have some functions that can revert with custom errors based on the provided arguments.

function dissmissRegulator(address _regulatorAddress) public {
    if (msg.sender != s_regulators[msg.sender].regulatorAddress) {
	revert EMR__OnlyRegulatorsCanAddOrRemoveRegulators();
    }

    if (s_regulators[_regulatorAddress].regulatorAddress != _regulatorAddress) {
	revert EMR__RegulatorDoesNotExist();
    }

    delete s_regulators[_regulatorAddress];
}

This seemed simple at first; I just need to use to.be.revertedWithCustomError in my unit tests. So I write this in my unit tests file:

it("something", async function () {
    expect(await emr.dissmissRegulator(regulator))
        .to.be.revertedWithCustomError(emr, "EMR__RegulatorDoesNotExist");
});

I expected this test to pass, shouldn't be complicated. But it doesn't. And the weird thing is the console writes out this output:

 Error: VM Exception while processing transaction: reverted with custom error 'EMR__RegulatorDoesNotExist()'
    at EMR.dissmissRegulator (contracts/EMR.sol:81)

Now I am confused, why is the test failing when the error string in the test matches the thrown error. Am I missing something? Is my logic flawed somehow?

英文:

I am trying to write some unit tests for solidity contract. I have some functions that can revert with custom error based on the arguments provided.

function dissmissRegulator(address _regulatorAddress) public {
    if (msg.sender != s_regulators[msg.sender].regulatorAddress) {
	revert EMR__OnlyRegulatorsCanAddOrRemoveRegulators();
    }

    if (s_regulators[_regulatorAddress].regulatorAddress != _regulatorAddress) {
	revert EMR__RegulatorDoesNotExist();
    }

    delete s_regulators[_regulatorAddress];
}

This seemed simple at first, i just need to use to.be.revertedWithCustomError.

So I write this in my unit tests file

it("something", async function () {
    expect(await emr.dissmissRegulator(regulator))
        .to.be.revertedWithCustomError(emr,"EMR__RegulatorDoesNotExist");
});

I expected this test to pass, shouldn't be complicated.

But it doesn't. And the weird thing is console writes out this output:

 Error: VM Exception while processing transaction: reverted with custom error 'EMR__RegulatorDoesNotExist()'
    at EMR.dissmissRegulator (contracts/EMR.sol:81)

Now I am confused, why is the test failing when the error string in test matches the thrown error. Am I missing something? Is my logic flawed somehow?

答案1

得分: 1

我等待了错误的表达方式。

英文:
await expect(emr.dissmissRegulator(regulator))
    .to.be.revertedWithCustomError(emr,"EMR__RegulatorDoesNotExist");

I awaited wrong expression

huangapple
  • 本文由 发表于 2023年6月1日 02:52:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定