0

I have set up a local geth on ubuntu and I'm running my project on it.

While writing test cases, I saw that the test cases which were working fine in testrpc environment started facing issues when run on local geth developement environment.

I don't get any error message from my test case when revert() is caught in my solidity code.

Here the example:

myContract {
   function ExpectedARevertWhenCalled(){
       revert();
   }
} 

Now when I call this method in my test. The test cases should fail but no error is thrown.

Can someone please help understand the issue behind it?

FYI: This is how I'm calling the method in my test case:

contract('MyContract', async () => {
  it("myContractFunction", async () => {
    let obj= await TwoKeyReg.new();
    await obj.ExpectedARevertWhenCalled();
  });
});
  • Don't know why it's not throwing, but you can try require(false); instead of revert(); – Maxpeinas Aug 23 '18 at 8:42
  • What is your test code? – Raghav Sood Aug 23 '18 at 8:43
  • Hi Raghav, kindly have a look at the code. I have added the test code as asked. I can assure you there's no problem with the code as I have been working with it very fine in ganache/testrpc environment. – Gagan Aug 23 '18 at 9:47
  • Hey Maxpeinas. Solidity code is working fine. The revert() is doing what it has to do. The problem is when I run the test cases. I wonder why test cases aren't failing when revert is read in private blockchain setup. On the other hand, same test cases failed when run with testrpc! :/ – Gagan Aug 23 '18 at 9:51
  • test case pass/failed depends on your assert/expected result, not from the actual result. – Ha ĐANG Aug 23 '18 at 9:56
1

If your test goal is expecting a revert() then you should do as following

Use revert helper method from openzepplin, named assertRevert.js

module.exports = async promise => {
  try {
    await promise;
    assert.fail('Expected revert not received');
  } catch (error) {
    const revertFound = error.message.search('revert') >= 0;
    assert(revertFound, `Expected "revert", got ${error} instead`);
  }
};

Import the revert to your test code

const assertRevert = require('assertRevert');

Then make your test code like below, mean that you expecting the method to throw a revert

contract('MyContract', async () => {
  it("myContractFunction", async () => {
    let obj= await TwoKeyReg.new();
    await assertRevert(obj.ExpectedARevertWhenCalled());
  });
});
  • Hi. I have implemented the solution given here: ethereum.stackexchange.com/questions/48627/… to catch revert type error in assert. It works quiet well with testrpc but when I ran the same code in my local-geth network, it throws an AssertionError saying "Expected an error but did not get one". This means I'm not at all receiving any kind of error from revert. – Gagan Aug 23 '18 at 10:34
  • when running on the local-geth, it might be that another error happen before reaching the revert; then we only know exactly if you provide fully code and your local environment setup – Ha ĐANG Aug 23 '18 at 10:55

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.