2

For instance I'm running testcase for a smart contract and it's test function has required validation.

function test(uint _id) public pure returns(bool){
    require(_id == 0 ,"not valid!");
    return true;
}

But when I execute the test case it throwing an error

AssertionError: Error: VM Exception while processing transaction: revert

Test cases:

it("Adding test into blockchain", async () => {                               

    const result = await Instance.test(1);                        

    expect(result).to.be.not.equal('null');            
});

it("Adding zero Id test into blockchain", async () => {                               

    const result = await Instance.test(0);                        

    //TODO: get error message   
});
2
  • what's your test block?
    – Badr Bellaj
    Jul 13, 2018 at 8:25
  • @BadrBellaj: question updated!
    – Hina
    Jul 13, 2018 at 8:32

2 Answers 2

1

Of course it will fire an exception

you have in the first test require(_id == 0 ,"not valid!");

and you feed it _id=1 (test(1)) therefore an exception will be raised.

either test with test(0)

or

check try/catch the exception as follow :

try {
     await Instance.test(1); 
        } catch (error) {
            Error = error;
        }
        assert.notEqual(Error, undefined, 'Error must be thrown');
        assert.isAbove(Error.message.search('VM Exception while processing transaction: revert'), -1, 'Error: VM Exception while processing transaction: revert');

    });
6
  • either test with test(0), but I want to create negative test(s) to make sure smart contract would handle the things appropriately. check try/catch the exception any example would be really helpful, currently I added catch block, though it gives an exception.
    – Hina
    Jul 13, 2018 at 10:33
  • I also tried to make library like this but seems import having issue with truffle.. don't know how other implemented.
    – Hina
    Jul 13, 2018 at 10:38
  • check the try/catch block i've proposed
    – Badr Bellaj
    Jul 13, 2018 at 10:40
  • 1
    what's awful in it? :p you raise an error and you check the message nothing harmfull
    – Badr Bellaj
    Jul 13, 2018 at 10:52
  • 2
    What if you have a stack of require statements and want to test which one triggered during a test. Without the message it seems impossible. Also, if you can't get the require message, what purpose does it even serve?
    – Justin
    Jul 13, 2018 at 14:59
0

you can either use truffle-assertions with

npm install truffle-assertions

in your test code

const truffleAssert = require('truffle-assertions');
await truffleAssert.reverts(Instance.test(1));

or you can use a simple try catch with

    try {
        await Instance.test(1);
    } catch(err) {
        Error = err
    }
    assert.equal(Error.reason, 'not valid!');

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.