Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Last active May 21, 2019 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/7e955f507e9ab46d8892d5203e6ff371 to your computer and use it in GitHub Desktop.
Save yuyasugano/7e955f507e9ab46d8892d5203e6ff371 to your computer and use it in GitHub Desktop.
Truffle Mocha MetaCoin Test refactoring
var MetaCoin = artifacts.require("./MetaCoin.sol");
contract('MetaCoin', (accounts) => {
describe('Token', () => {
it("should put 10000 MetaCoin in the first account", async () => {
let instance = await MetaCoin.deployed();
let alice = accounts[0];
let balance = await instance.getBalance.call(alice);
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
});
it("should send coin correctly", async () => {
// Get initial balances of first and second account.
let alice = accounts[0];
let bob = accounts[1];
var amount = 10;
let instance = await MetaCoin.deployed();
let meta = instance;
let balance = await meta.getBalance.call(alice);
let alice_starting_balance = balance.toNumber();
balance = await meta.getBalance.call(bob);
let bob_starting_balance = balance.toNumber();
await meta.sendCoin(bob, amount, { from: alice });
balance = await meta.getBalance.call(alice);
let alice_ending_balance = balance.toNumber();
balance = await meta.getBalance.call(bob);
let bob_ending_balance = balance.toNumber();
assert.equal(alice_ending_balance, alice_starting_balance - amount, "Amount wasn't correctly taken from the sender");
assert.equal(bob_ending_balance, bob_starting_balance + amount, "Amount wasn't correctly sent to the receiver");
});
});
describe('Library', () => {
it("should call a function that depends on a linked library", async () => {
let alice = accounts[0];
let meta = await MetaCoin.deployed();
let outCoinBalance = await meta.getBalance.call(alice);
let metaCoinBalance = outCoinBalance.toNumber();
let outCoinBalanceEth = await meta.getBalanceInEth.call(alice);
let metaCoinEthBalance = outCoinBalanceEth.toNumber();
assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, "Library function returned unexpected function, linkage may be broken");
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment