1

In Truffle; I am trying to modify the already existing MetaCoin sample unit test in order to better understand how everything works. Below is the unit test they provide which transfers 10 coins from account[0] to account[1] which passes.

  it("should send coin correctly", function() {
    var meta;

    //    Get initial balances of first and second account.
    var account_one = accounts[0];
    var account_two = accounts[1];

    var account_one_starting_balance;
    var account_two_starting_balance;
    var account_one_ending_balance;
    var account_two_ending_balance;

    var amount = 10;

    return MetaCoin.deployed().then(function(instance) {
      meta = instance;
      return meta.getBalance.call(account_one);
    }).then(function(balance) {
      account_one_starting_balance = balance.toNumber();
      return meta.getBalance.call(account_two);
    }).then(function(balance) {
      account_two_starting_balance = balance.toNumber();
      return meta.sendCoin(account_two, amount, {from: account_one});
    }).then(function() {
      return meta.getBalance.call(account_one);
    }).then(function(balance) {
      account_one_ending_balance = balance.toNumber();
      return meta.getBalance.call(account_two);
    }).then(function(balance) {
      account_two_ending_balance = balance.toNumber();

      assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender");
      assert.equal(account_two_ending_balance, 10, "Amount wasn't correctly taken from the sender");
    });
  });

I created a second unit test; where I wanted to transfer 5 coins out of account[1] and transfer it to account[2]; however, my test always returns 0.

  it("should send some more coins!", function() {
      var amount = 5;
      var meta;
      var account_two = accounts[1];
      var account_three = accounts[2];

      return MetaCoin.deployed().then(function(instance) {
        meta = instance;
        return instance.sendCoin.call(account_three, amount, {from: account_two});
    }).then(function(balance) {
      return meta.getBalance.call(account_three);
    }).then(function(balance) {
      assert.equal(balance.toNumber(), amount, "Amount equaled 5 as expected");
    });
  })

When I run it; the unit test results are this:

Contract: MetaCoin √ should put 10000 MetaCoin in the first account √ should call a function that depends on a linked library (68ms) √ should send coin correctly (156ms) 1) should send some more coins!

3 passing (390ms) 1 failing

1) Contract: MetaCoin should send some more coins!: AssertionError: Amount equaled 5 as expected: expected 0 to equal 5 at C:/Dev/Blockchain/test/metacoin.js:92:14 at process._tickCallback (internal/process/next_tick.js:103:7)

  • I think it should be instance.sendCoin(account_three, amount, {from: acccount_two});, ie without call. – Ismael Jun 12 '17 at 16:46
  • @Ismael - Ahh... that was it! Thank you! Can you tell me, what is the .call function used for and when do you know when to use it? Is call a Truffle or Solidity function? Also--if you want to put this as the answer, I will mark it for you! – Flea Jun 12 '17 at 18:48
2

I think it should be

instance.sendCoin(account_three, amount, {from: acccount_two});

ie without the call part.

It is used to differentiate when you are calling a constant method vs calling a non-constant method. I think it is from web3 which is the library that interact with your geth/parity node.

Constant methods do not use gas when being called (they do not generate a transaction), they cannot modify the contract storage, and they can return a value as a result of the operation.

Non-constant methods use gas when invoked (ie they generate a transaction in the blockchain), and can modify your contract storage, the usually will not return value as a result.

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.