0

I'm trying to test the balance of the contract with truffle, but I'm obviously doing something wrong as the test passes.

it("should check balance", function() {
  Lottery.deployed().then(function(instance){
    return instance.balance.call();
  }).then(function(balance) {
    assert.equal(balance, balance - 1, "Balance incorrect!");      
  })

What is wrong with the test?

Contract is basically empty.

pragma solidity ^0.4.17;

contract Lottery{
    address public owner;
    uint public fee;
    uint public jackpot;

    constructor() public {
        owner = msg.sender;
        jackpot = 0;
        fee = 1 finney
    }

    //buy ticket
    function buyTicket() public payable {
        require(msg.value == 1 finney, "Incorrect fee");

        jackpot += 1;
    }

    function () external payable {

    }
}
  • 1
    It would be helpful if you provided the solidity code as well since the test calls your solidity code and then passes the test parameters there. – R.D Sep 28 '18 at 10:02
  • Please have a look for example at OpenZeppelin's crowdsale tests: github.com/OpenZeppelin/openzeppelin-solidity/blob/master/test/… – Lauri Peltonen Sep 28 '18 at 10:04
  • I added the contract, though it's basically empty at the moment. – typhon04 Sep 28 '18 at 10:10
  • Can you console.log balance before the assert? – Shawn Tabrizi Sep 28 '18 at 10:12
  • Your code is incomplete. You are calling the balance function/variable and assigning it an assert in the test code but there is no balance in the solidity code. Your test code has little to no relation with the solidity code so if you try to run it, it will not find anything at all so the assert does not function at all and by default the test succeeds since it is checking/assigning something that doesn't exist. Under normal circumstances it would throw an error but the solidity version mentioned in the code is so old that I cannot be sure. Use the latest version and write the balance function – R.D Sep 28 '18 at 10:19
1

This should help you getting started:

it("should check balance", async function() {
    let lottery = await artifacts.require("Lottery.sol").new();
    let balance = web3.eth.getBalance(lottery.address);
    console.log(balance.toFixed());
});

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.