0

So I have this setup : truffle and ganache-cli

I'm sending some ether to my contract, here is the related part of my contract:

    mapping(address => uint256) public balanceOf;

    function () payable public {
              uint amount = msg.value;
              balanceOf[msg.sender] += amount;
        }

In truffle this is how I send the ether.

it("Test if can be payed", function(){
    return web3.eth.sendTransaction({
           from:fromAddr, 
           to:MyContract.address,  
           value:amountToSend
    }).then(function(res){  
           expect(res).to.not.be.an("error"); // test passed
    });
 });

it("Test if contract received ether", function(){
        return web3.eth.getBalance(MyContract.address, 
               function(err, res){
                        expect(parseInt(res)).to.be.at.least(1000000000000000000); // test passed
                });
});

it("Catch if balanceOf "+fromAddr, function(){
        return sale.balanceOf.call(fromAddr).then(function(res){
                        expect(parseInt(res)).to.be.at.least(1); // fails the test
               });
});

Am I doing it right? What could be the reason for failed test? truffle test output :

AssertionError: expected 0 to be at least 1
      + expected - actual

      -0
      +1

I can provide more info if needed.

  • Checking if parseInt(res) returns 1000000000000000000 may possibly fail, since Javascript's max safe integer (Number.MAX_SAFE_INTEGER) is much smaller (2 ^ 53 - 1 to be accurate). – goodvibration Dec 21 '18 at 8:16
  • In general, don't use parseInt on res. If I'm not mistaken, then res is a BigNumber (or a BN in later versions of web3.js). This class has all the functionality that you need, for example, greaterThan, equals, etc. If I'm wrong and res is a string, then convert it to a BigNumber. – goodvibration Dec 21 '18 at 8:18
  • In general, I'd replace expect(parseInt(res)).to.be.at.least(1000000000000000000) with assert(res.greaterThanOrEqualTo("1000000000000000000")), and expect(parseInt(res)).to.be.at.least(1) with assert(res.greaterThanOrEqualTo("1")). – goodvibration Dec 21 '18 at 8:24
  • I've changed as per your suggestion, but the same case fail – Cԃաԃ Dec 21 '18 at 8:45
  • 1
    Yes, though your terminology is wrong; Truffle is a testing framework (based on Mocha framework for it, before, after, etc). Web3 is a protocol for communicating with an Ethereum node (Ganache in your case). With sale.balanceOf.call, you are not "doing it with Truffle". The call is eventually translated into a web3 message to Ganache. If you want to send ether in a "normal" function call (i.e., without web3.eth.sendTransaction), then just give a name to your fallback function (currently declared as function () payable public), and call it the same way you call function balanceOf. – goodvibration Dec 21 '18 at 9:55
1

As I've mentioned in the comments, there are a number of problems in your code:

  • Calling parseInt(res) may lead to an incorrect result, since res is a BigNumber possibly larger than Number.MAX_SAFE_INTEGER
  • Using 1000000000000000000 may lead to an incorrect result, since it is larger than Number.MAX_SAFE_INTEGER

I'm not sure what exactly the cause of your failure, since I always use async/await instead of then where possible.

But in any case, this seems to be working fine:

contract("Sale", function(accounts) {
    let sale;
    let fromAddr = accounts[0];
    let amountToSend = "1000000000000000000";

    before(async function() {
        sale = await artifacts.require("Sale").new();
    });

    it("Test if can be payed", async function() {
        let res = await web3.eth.sendTransaction({
            from: fromAddr, 
            to: sale.address,  
            value: amountToSend
        });
        // if we reach here then the transaction has completed successfully
    });

    it("Test if contract received ether", async function() {
        let res = await web3.eth.getBalance(sale.address);
        assert(res.greaterThanOrEqualTo(amountToSend));
    });

    it("Catch if balanceOf "+fromAddr, async function() {
        let res = await sale.balanceOf.call(fromAddr);
        assert(res.greaterThanOrEqualTo(1));
    });
});
  • Could you please tell me if I need to add a module for greaterThanOrEqualTo ? TypeError: res.greaterThanOrEqualTo is not a function this what I get when I use your code. – Cԃաԃ Dec 21 '18 at 10:56
  • Maybe the problem is with my setup, but using async/await did not solve my issue – Cԃաԃ Dec 21 '18 at 11:48
  • What version of BigNumber are you using? Or more precisely, what version of web3 are you using (since web3 imports BigNumber into your system)? Or more precisely, what version of truffle are you using (since truffle imports web3 into your system)? You possibly need to change greaterThanOrEqualTo to isGreaterThanOrEqualTo. – goodvibration Dec 21 '18 at 12:10

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.