英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论