我正在为彩票合同编写测试,并且我正在测试一个应该因自定义错误而失败的场景。我还在学习区块链,所以任何帮助都会很感激。
下面是固体代码:
function performUpkeep(
bytes calldata /* performData*/
) external override {
(bool upkeepNeeded, ) = checkUpkeep("");
if (!upkeepNeeded) {
revert Raffle__UpkeepNotNeeded(
address(this).balance,
s_players.length,
uint256(s_raffleState)
);
}
s_raffleState = RaffleState.CALCULATING;
uint256 requestId = i_vrfCoordinator.requestRandomWords(
i_gasLane, //
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUM_WORDS
);
emit RequestedRaffleWinner(requestId);
}
这是我的测试脚本:
describe("performUpkeep", function () {
it("can only run if checkupkeep is true", async () => {
await raffle.enterRaffle({ value: raffleEntranceFee })
await network.provider.send("evm_increaseTime", [interval.toNumber() + 1])
await network.provider.request({ method: "evm_mine", params: [] })
const tx = await raffle.performUpkeep("0x")
assert(tx)
})
it("reverts if checkup is false", async () => {
await expect(raffle.performUpkeep("0x")).to.be.revertedWith(
`Raffle__UpkeepNotNeeded(0,0,0)`
)
})
})
我得到以下错误:
AssertionError: Expected transaction to be reverted with Raffle__UpkeepNotNeeded(0,0,0), but other exception was thrown: Error: VM Exception while processing transaction: reverted with custom error 'Raffle__UpkeepNotNeeded(0, 0, 0)'
双引号和反引号都会产生相同的错误。
我很难在chai网站https://ethereum-waffle.readthedocs.io/en/latest/matchers.html#revert-with-message和stackoverflow上找到任何例子。
一个简单的插值修复修复了这个问题。
await expect(raffle.performUpkeep("0x")).to.be.revertedWith(
`Raffle__UpkeepNotNeeded(${0}, ${0}, ${0})`
如果不将它们分开,错误将持续存在。
来源:github论坛