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?
        });
    });
});
5
  • I tried both way! still it gave "Uncaught Error: VM Exception while processing transaction: revert" instead of expected error! @goodvibration Sep 25, 2018 at 12:56
  • Refer the following question stackoverflow.com/questions/30367395/… Sep 25, 2018 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, 2018 at 16:29
  • it's test code!! @Utgarda Sep 26, 2018 at 6:28
  • Well, then using OpenZeppelin helpers is your best shot, check my answer!
    – Utgarda
    Sep 26, 2018 at 8:45

2 Answers 2

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(...);
5
  • 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 Sep 26, 2018 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, 2018 at 12:50
  • i have already installed openzeppelin then imported it's file!! @Utgarda Sep 26, 2018 at 13:37
  • And what did it tell you, apart from "Error"? Try it without the curly braces.
    – Utgarda
    Sep 26, 2018 at 13:40
  • it gave error==> import expectThrow from "./node_modules/openzeppelin-solidity/test/helpers/expectThrow.js"; ^^^^^^^^^^^ SyntaxError: Unexpected identifier @Utgarda Sep 27, 2018 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
});

});
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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