0
votes

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.

UPDATE : for clarification on sale which is global variable.

   it("Test if MyContract is deployed", function(){
            return MyContract.deployed().then(function(instance){
                   sale = instance;
            });
   });
1

1 Answers

1
votes

I think this is what you are looking for:

File path: test/vault.js

const Vault = artifacts.require("Vault");

contract("Vault test", async accounts => {

    // Rely on one instance for all tests.
    let vault;
    let fromAccount   = accounts[0];
    let oneEtherInWei = web3.utils.toWei('1', 'ether');

    // Runs before all tests.
    // https://mochajs.org/#hooks
    before(async () => {
        vault = await Vault.deployed();
    });

    // The `receipt` will return boolean.
    // https://web3js.readthedocs.io/en/1.0/web3-eth.html#gettransactionreceipt
    it("Test if 1 ether can be paid", async () => {
        let receipt = await web3.eth.sendTransaction({
            from:  fromAccount, 
            to:    vault.address, 
            value: oneEtherInWei
        });
        expect(receipt.status).to.equal(true);
    });

    it("Test if contract received 1 ether", async () => {
        let balance = await web3.eth.getBalance(vault.address);
        expect(balance).to.equal(oneEtherInWei);
    });

    // In Web3JS v1.0, `fromWei` will return string.
    // In order to use `at.least`, string needs to be parsed to integer.
    it("Test if balanceOf fromAccount is at least 1 ether in the contract", async () => {
        let balanceOf    = await vault.balanceOf.call(fromAccount);
        let balanceOfInt = parseInt(web3.utils.fromWei(balanceOf, 'ether'));
        expect(balanceOfInt).to.be.at.least(1);
    });
});

You can see the full project here. Do note that I'm using Truffle v5 and Ganache v2. See the README file inside that GitLab repository.

Back to your question, there were 2 mistakes:

  1. The sale is not defined. I have a feeling that you were actually referring to MyContract.

  2. In order to use the least method in ChaiJS, you need to make sure you are passing integers. The balanceOf call is returning BigNumber or BN object and you can't use it with .least method.

FYI, Truffle v5 is now using BN by default (previously BigNumber). More about it here.

Let me know if this helps.