0

test.js

var Test=artifacts.require("./Test.sol");

contract('Test', function(accounts) {
    it("test case-1 working", function(){
        var admin_address=accounts[0]; //admin account initialized with 100

        var testit;
        return Test.deployed().then(function(instance){
            testit=instance;
            return testit.balanceOf(admin_address);
        }).then(function(result){
            console.log("admin balance==>",result.valueOf());
            testit.transfer(admin_address,50,{from:admin_address});
            //The error occurs in the line above. How to catch it?
        });
    });
});
  • I tried both way! still it gave "Uncaught Error: VM Exception while processing transaction: revert" instead of expected error! @goodvibration – Sunanda Sharma Sep 25 '18 at 12:56
  • Refer the following question stackoverflow.com/questions/30367395/… – Soham Lawar Sep 25 '18 at 12:57
  • Hi Sunanda! Is that meant as test code, or as main logic for contract interaction from JS? And if it's not test code with Truffle, what's your web3js version? – Utgarda Sep 25 '18 at 16:29
  • it's test code!! @Utgarda – Sunanda Sharma Sep 26 '18 at 6:28
  • Well, then using OpenZeppelin helpers is your best shot, check my answer! – Utgarda Sep 26 '18 at 8:45
0

If you need to test in JS that your contract throws when it's supposed to, have a look at OpenZeppelin's helpers. To check that your contract throws exceptions when it's expected to do so, try expectThrow

import expectThrow from 'openzeppelin-solidity/test/helpers/expectThrow';
...
}).then(function () {
    return expectThrow(testit.transfer(admin_address,50,{from:admin_address}));
}).then(function () {
...

In case you need to actually catch exceptions from contract method calls in your main, non-test JS code with web3js, try this:

testit
    .transfer(admin_address,50,{from:admin_address})
    .then(...)
    .catch(function (ex) {...} );
    .then(...);
| improve this answer | |
  • I have to expect throw in test.js file. this "import {expectThrow} from 'openzeppelin-solidity/test/helpers/expectThrow'; " gave error when I used in test.js. @Utgarda – Sunanda Sharma Sep 26 '18 at 11:32
  • Please share that message too ;) But to make it work, you need to have OpenZeppelin files, you can't just import anything from the internet. github.com/OpenZeppelin/openzeppelin-solidity#install – Utgarda Sep 26 '18 at 12:50
  • i have already installed openzeppelin then imported it's file!! @Utgarda – Sunanda Sharma Sep 26 '18 at 13:37
  • And what did it tell you, apart from "Error"? Try it without the curly braces. – Utgarda Sep 26 '18 at 13:40
  • it gave error==> import expectThrow from "./node_modules/openzeppelin-solidity/test/helpers/expectThrow.js"; ^^^^^^^^^^^ SyntaxError: Unexpected identifier @Utgarda – Sunanda Sharma Sep 27 '18 at 4:44
-1

First I have installed openzeppelin-solidity! after it have import expectThrow.js file in a way given below!! expectThrow.js is available in oppenzeppelin-solidity/test/helpers/expectThrow.js !

testit.js

var Test=artifacts.require("./Test.sol");

contract('Test', function(accounts) {
var catcherror=require("../node_modules/openzeppelin-solidity/test/helpers/expectThrow.js").expectThrow;


    it("test case-1 working", function(){
    var admin_address=accounts[0]; //admin account initialized with 100

var testit;
    return Test.deployed().then(function(instance){
    testit=instance;
    return testit.balanceOf(admin_address);
    }).then(function(result){

   console.log("admin balance==>",result.valueOf());
catcherror(transfer(admin_address,50,{from:admin_address}));//here I catched error using expectThrow.js
});

});
});
| improve this answer | |

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.