简体   繁体   中英

Hardhat how to use revertedWith

I'm writing tests for an ERC-20 contract and I'm testing a scenario that should fail with the (OpenZeppelin library-provided) error:

revert ERC20: burn amount exceeds balance

In my terminal output I see:

ProviderError: VM Exception while processing transaction: revert ERC20: burn amount exceeds balance

However when I try the code I can't get test to succeed:

 it("Address #1 can't burn 100M tokens", async function () {
    await expect(contract.connect(signer1)
      .burn(parseEther((100_000_000).toString())))
      .revertedWith('revert ERC20: burn amount exceeds balance')
        });

I've also tried replacing the string argument to revertedWith with "burn amount exceeds balance", "VM Exception while processing transaction: revert ERC20: burn amount exceeds balance", "ProviderError: VM Exception while processing transaction: revert ERC20: burn amount exceeds balance", and "burn amount exceeds balance" yet all of them are failing.

What exact error reason should I be using with revertedWith assertion while testing hardhat with chai? (I'm using standard OpenZeppelin 4 ERC-20 contracts)

I believe that Hardhat is using the same matchers as Waffle. And if you follow the link , you can see that the proper syntax would be

 it("Address #1 can't burn 100M tokens", async function () {
    await expect(contract.connect(signer1)
      .burn(token_amount)
      .to.be.revertedWith('revert ERC20: burn amount exceeds balance')
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM