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)
instance.sendCoin(account_three, amount, {from: acccount_two});
, ie withoutcall
.