What is the pattern for handling throw on a Solidity contract in tests

+1 vote

I have a function on a Solidity contract that does a throw. Eg.

   function do(x,y)  {
        if ( msg.sender != owner )
            throw;
        // ...
   }

In the Truffle environment I have a test js something like:

//.... part of a promise chain
       .then(
            function (_bool0) {
                assert.isTrue(_bool0,"whoops - should be true");
                return contract.do( "okdoke" , {from: accounts[1]} );
            }).then(
            function (tx_id) {
                //..
                done();
            }
    // ...

The return contract.do() causes the condition that results in the throw. Which produces the following in the Truffle test output for this test:

Error: VM Exception while executing transaction: invalid JUMP
Sep 24, 2018 in Blockchain by slayer
• 29,370 points
1,821 views

3 answers to this question.

0 votes

The zeppelin project as an awesome way to do just that:

it("should fail to withdraw", async () => {
    try {
      await receiver.withdrawToken(0x0);
      assert.fail('should have thrown before');
    } catch(error) {
      assertJump(error);
    }
  });

function assertJump(error) {
  assert.isAbove(error.message.search('invalid opcode'), -1, 'Invalid opcode error must be returned');
}

https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/test/Ownable.js To see a full example

answered Sep 24, 2018 by digger
• 26,740 points
0 votes

Just to let everyone know, I came across this problem as well and have been using the following:

function getTransactionError(func) {
  return Promise.resolve().then(func)
    .then(function(txid) {
      var tx = web3.eth.getTransaction(txid);
      var txr = web3.eth.getTransactionReceipt(txid);
      if (txr.gasUsed === tx.gas) throw new Error("all gas used");
    })
    .catch(function(err) {
      return err;
    });
}
answered Sep 25, 2018 by Judy
0 votes

In my opinion the cleanest way is:

it("should revert", async function () {
    try {
        await deployedInstance.myOperation1();
        assert.fail("The transaction should have thrown an error");
    }
    catch (err) {
        assert.include(err.message, "revert", "The error message should contain 'revert'");
    }
});
answered Sep 25, 2018 by Lupin

Related Questions In Blockchain

0 votes
1 answer
0 votes
1 answer

What is the difference between if() and require() statement in solidity??

If() and require() have separate functions and ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,400 points
5,073 views
+1 vote
1 answer

What is the best way to search for an item in blockchain?

All transactions and records in blockchain are ...READ MORE

answered Apr 21, 2018 in Blockchain by Perry
• 17,100 points
1,929 views
0 votes
1 answer

What could be the best term to use for the collection of contracts in a .sol file?

module - don't think so. Because module ...READ MORE

answered Jun 2, 2018 in Blockchain by Shashank
• 10,400 points
746 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,941 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
45,289 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,466 views
0 votes
1 answer

What is the peer node start in hyperledger?

Nodes are the fundamental element of the ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 26,740 points
1,057 views
0 votes
1 answer

Web3 1.0: What does `web3.eth.call(tx)` return for a contract creation?

The returned bytestring is the deployedBytecode of the contract, ...READ MORE

answered Sep 17, 2018 in Blockchain by digger
• 26,740 points
1,231 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP