Created
May 22, 2019 10:22
-
-
Save yuyasugano/b4b83b47a4853b0e8fd962c25381507d to your computer and use it in GitHub Desktop.
Truffle Mocha MetaCoin Test refactoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MetaCoin = artifacts.require("./MetaCoin.sol"); | |
const utils = require('./utils.js'); | |
contract('MetaCoin', (accounts) => { | |
describe('Token', () => { | |
let balance; | |
let instance; | |
const [alice, bob] = accounts; | |
before(async () => { | |
instance = await MetaCoin.deployed(); | |
}); | |
it("should put 10000 MetaCoin in the first account", async () => { | |
let balance = await instance.getBalance.call(alice); | |
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); | |
}); | |
it("should fail if the user does not have enough MetaCoin", async () => { | |
let amount = 10000; | |
try { | |
await instance.sendCoin(alice, amount, { from: bob }); | |
} catch (err) { | |
assert(utils.isEVMException(err), err.toString()); | |
let bob_ending_balance = await utils.getTokenBalance(bob, instance); | |
assert.equal(bob_ending_balance, 0, "0 wasn't in the second account"); | |
return; | |
} | |
assert(false, '10000 was in the second account'); | |
}); | |
it("should send coin correctly", async () => { | |
// Get initial balances of first and second account. | |
let amount = 10; | |
let alice_balance = await utils.getTokenBalance(alice, instance); | |
let bob_balance = await utils.getTokenBalance(bob, instance); | |
await instance.sendCoin(bob, amount, { from: alice }); | |
let alice_ending_balance = await utils.getTokenBalance(alice, instance); | |
let bob_ending_balance = await utils.getTokenBalance(bob, instance); | |
assert.equal(alice_ending_balance, alice_balance - amount, "Amount wasn't correctly taken from the sender"); | |
assert.equal(bob_ending_balance, bob_balance + amount, "Amount wasn't correctly sent to the receiver"); | |
}); | |
it("should not send coin to zero address", async () => { | |
let amount = 10; | |
let receiver = 0x0000000000000000000000000000000000000000; | |
let alice_balance = await utils.getTokenBalance(alice, instance); | |
try { | |
await instance.sendCoin(receiver, amount, { from: alice }); | |
} catch (err) { | |
assert(utils.isEVMException(err), err.toString()); | |
let alice_ending_balance = await utils.getTokenBalance(alice, instance); | |
assert.equal(alice_ending_balance, alice_balance, "Amount wasn't the same for the sender"); | |
return; | |
} | |
assert(false, 'sendCoin sent coin to zero address'); | |
}); | |
}); | |
describe('Library', () => { | |
it("should call a function that depends on a linked library", async () => { | |
let alice = accounts[0]; | |
let instance = await MetaCoin.deployed(); | |
let {metaCoinEthBalance, metaCoinBalance} = await utils.getTokenEthBalance(alice, instance); | |
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