Skip to content

Instantly share code, notes, and snippets.

@sogoiii
Last active March 22, 2017 23:28
Show Gist options
  • Save sogoiii/d3b512aab0623d4ad1da5c5bde3d3323 to your computer and use it in GitHub Desktop.
Save sogoiii/d3b512aab0623d4ad1da5c5bde3d3323 to your computer and use it in GitHub Desktop.
Truffle 3.0 tests in async await
var MetaCoin = artifacts.require("./MetaCoin.sol");
contract('MetaCoin', function(accounts) {
it("should put 10000 MetaCoin in the first account", async function() {
let meta = await MetaCoin.deployed();
let balance = await meta.getBalance.call(accounts[0]);
assert.equal(balance.valueOf(), 10000, "10000 wasn't in teh first account")
});
it("should call a function that depends on a linked library", async function() {
let meta = await MetaCoin.deployed();
let outCoinBalance = await meta.getBalance.call(accounts[0]);
let outCoinBalanceEth = await meta.getBalanceInEth.call(accounts[0]);
let metaCoinBalance = outCoinBalance.toNumber();
let metaCoinEthBalance = outCoinBalanceEth.toNumber();
assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, "Library function returned unexpected function, linkage may be broken");
});
it("should send coin correctly", async function() {
// Get initial balances of first and second account.
var account_one = accounts[0];
var account_two = accounts[1];
var amount = 10;
let meta = await MetaCoin.deployed();
let balance1 = await meta.getBalance.call(account_one);
let balance2 = await meta.getBalance.call(account_two);
let account_one_starting_balance = balance1.toNumber();
let account_two_starting_balance = balance2.toNumber();
await meta.sendCoin(account_two, amount, {from: account_one});
let balance3 = await meta.getBalance.call(account_one);
let balance4 = await meta.getBalance.call(account_two);
let account_one_ending_balance = balance3.toNumber();
let account_two_ending_balance = balance4.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, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment