Created
May 21, 2019 11:15
-
-
Save yuyasugano/92eff868a86eca0abf164bda8555c391 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
var MetaCoin = artifacts.require("./MetaCoin.sol"); | |
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 send coin correctly", async () => { | |
// Get initial balances of first and second account. | |
var amount = 10; | |
balance = await instance.getBalance.call(alice); | |
let alice_balance = balance.toNumber(); | |
balance = await instance.getBalance.call(bob); | |
let bob_balance = balance.toNumber(); | |
await instance.sendCoin(bob, amount, { from: alice }); | |
balance = await instance.getBalance.call(alice); | |
let alice_ending_balance = balance.toNumber(); | |
balance = await instance.getBalance.call(bob); | |
let bob_ending_balance = balance.toNumber(); | |
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"); | |
}); | |
}); | |
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