How can I test the withdraw()
function in a truffle javascript test case for the below contract?
The problem is, If I use the msg.sender
from the same contract, the before and after balance will be same.
Is there a way to create a new test contract and use it to call the withdraw
function from the javascript test case?
pragma solidity ^0.4.18;
import "./ConvertLib.sol";
contract MetaCoin {
mapping (address => uint) balances;
event NewDeposit(address _from, uint256 _value);
function MetaCoin() public {
balances[tx.origin] = 10000;
}
function getBalance(address addr) public view returns(uint) {
return balances[addr];
}
function deposit() payable {
balances[msg.sender] += msg.value;
NewDeposit(msg.sender, msg.value);
}
function withdraw() {
if ( ! (msg.sender.call.value(balances[msg.sender])() ) ) {
throw;
}
balances[msg.sender] = 0; // Please don't worry about re-entrancy for now.
}
}