...by Daniel Szego
quote
"Simplicity is the ultimate sophistication."
Leonardo da Vinci

Sunday, January 7, 2018

Solidity and Truffle Tips and Tricks - testing errors as unit tests


Supposing that you have to write a solidity unit test and you expect the result as an error message, you can use the following pattern, simply using catch in the unit testing instead of or beside then

contract('Contract', function(accounts) {
 it("test Contract from wrong account", function() {
  return Contract.deployed().then(function(instance) {
   return Contract.callFunction(<params>); 
  }).then(function(balance) {
   assert(false, "Call should not be allowed");            
  }).catch(function(error) {
   errorMessage = error.toString();
   if (errorMessage.indexOf("invalid opcode")  > 0){
    assert(true, "Call should not be allowed - error as expected");     }
    else{
   assert(true, "Call should not be allowed - wrong message");   
    }
   });
  });
});