OpenZeppelin / openzeppelin-test-helpers

Assertion library for Ethereum smart contract testing
https://docs.openzeppelin.com/test-helpers
MIT License
416 stars 132 forks source link

Difficult to match revert messages #183

Open daltonclaybrook opened 2 years ago

daltonclaybrook commented 2 years ago

I'm trying to test using expectRevert and assert on a specific error message:

await expectRevert(
    lock.collect(1, accounts[1], max, max, { from: accounts[1] }),
    'Not authorized'
);

I'm expecting the error message to be "Not authorized" because that is the exact text I use in my require condition in my contract. Instead, I'm getting this message:

Transaction: 0x2795a7ddf35519454e294df60a84efccc4b41d906423efef4f1a41ec91a02389 exited with an error (status 0). Reason given: Not authorized.
     Please check that the transaction:
     - satisfies all conditions set by Solidity `require` statements.
     - does not trigger a Solidity `revert` statement.

This message is a bit more difficult to match against because, in addition to the several newline and whitespace characters, it also contains the transaction hash which may be different between test runs.

Is this a bug, or perhaps a change in later versions of the Solidity compiler? The docs for expectRevert don't seem to mention this kind of verbose error message. Is there anything I can do differently to get around this? One thought I had was to allow passing a predicate function in place of the string so I could check the string within the function and return true or false if it matches my expectation, e.g.

await expectRevert(
    lock.collect(1, accounts[1], max, max, { from: accounts[1] }),
    (str) => str.indexOf('Reason given: Not authorized') != -1
);
frangio commented 2 years ago

I believe this is due to a change in the latest version of Ganache.