I am writing a test suite to ensure that parameters in my smart contract's constructor are being properly validated by a require() statement prior to any gas expenditures.

I have been looking through truffle's documentation but all the examples thus far relate to calling functions of successfully instantiated contracts with require() clauses.

My code is as follows:

contract MyContract is ERC20 {
    using SafeMath for uint256;
        uint256 total, 
        uint256 constituent, 
        uint256 otherConstituent
    ) public ERC20("Name", "SMBL") {
        require(constituent.add(otherConstituent) <= total, "error!");

        // Other code

Is it even possible to test this case using Truffle's tooling?

  • Truffle, and other Solidity SDKs, can catch reverts and the relevant messages. Thus, your test will test for the revert message presence in the deployment transaction. How do to this with Truffle, please look up the documentation as I do not program in Truffle anymore myself. Jun 22 at 8:33

I ended up using open zepellin's test helpers library:

const { expectRevert } = require("@openzeppelin/test-helpers");

contract("MyContract Tests", async (accounts) => {

    it("Tests a throw", async () => {
        await expectRevert.unspecified(
            MyContract.new(100, 200, 3);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.