0

Say I have a contract:

contract Reverts{
    constructor(param1){
        require(param1);
    }
}

If param1 is false, the constructor should fail and revert state.

truffle-assertions allows me to check if an existing contract abstraction's function will revert using await truffleAssertions.reverts(Reverts.someFunc());

That doesn't apply to constructors, since they are what make the contract abstraction in the first place.

How can I achieve this functionality with a constructor, instead of a regular function?

In a Truffle JavaScript testing file:

const truffleAssertions = require("truffle-assertions");
const Reverts = artifacts.require("Reverts");
let reverts;

contract("Reverts", (accounts) => {
    it("Check that param1=false, constructor reverts.", async () =>{
        // What to do here?
    });
});
1
  • 1
    Use a try/catch block
    – Julissa DC
    Jun 23 at 19:21

1 Answer 1

0

For this example contract, I used:

contract("Reverts", (accounts) => {
    it("Check that param1=false, constructor reverts.", async () =>{
        try{
            await Reverts.new(false);
        }
        catch{return;}
        assert.fail("Constructor does not revert when param1=false.");
    });
});

If the constructor reverts, the test simply returns and shows as passing. If it does not revert, then the assert statement is reached, causing an fail.

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.