0

I am creating test case of solidity in truffle and my solidity function is given below for which I am creating test cases

function isValidBuyer(uint256 buyTokenQty, uint256 senderValue) public  constant returns (bool)
    {
        if(!isUserExist(msg.sender))
        {
            msg.sender.transfer(senderValue); //Refund Ether
            return false ; 
        }
        else 
        {
            return true ;
        }
    }

And my test cases is as given below

it('TCC001: should check invalid buyer and should return false',   async () => {
let tempRes1=await instChain.isValidBuyer(100,1,{from:account2,gas:'220000'});

 assert.isTrue(!tempRes1);
}); 

While testing if clause of function in this test I am getting error

VM Exception while processing transaction: revert

One interesting case is in function isValidBuyer() when I am passing 1 wei it gives above mentioned exception but when I send 0.99 or less it gets execute successfully without error.

2

It isn't very clear here what you are trying to achieve. It seems you try to refund token or ether that hasn't been transfered yet ?

But, here is a few notes about your code:

i. If you transfer ether, you don't need to specify the amount as function argument. But you need to add the keyword payable and transfer the value like that:

instChain.isValidBuyer(100,1,{from:account2,gas:'220000',value:<AMOUNT IN WEI>});

ii. You are using the keyword constant for a state changing operation (write operation) and it cannot work because this keyword is exclusively for read-only function, you can't invoke an operation that changes the state (like transfer fund or store data) in this kind of function.

iii. you would simply use require to check if the member exist and refund the amount passed to the contract + remaining gas.

function isValidBuyer(uint256 buyTokenQty, uint256 senderValue) public  payable returns (bool) {
    // access value with `msg.value`

    require(isUserExist(msg.sender)) {

    return true
}
  • Your assumption is right. I want to transfer back ether if condition is not met. As you suggested to use require but the problem is when condition is met it returns true but if condition is not met it throws exception which I don't want, instead it should return false. – Ritesh Gupta Mar 21 '18 at 11:04

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.