metacoin.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. contract('MetaCoin', function(accounts) {
  2. it("should put 10000 MetaCoin in the first account", function() {
  3. var meta = MetaCoin.deployed();
  4. return meta.getBalance.call(accounts[0]).then(function(balance) {
  5. assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
  6. });
  7. });
  8. it("should call a function that depends on a linked library ", function(){
  9. var meta = MetaCoin.deployed();
  10. var metaCoinBalance;
  11. var metaCoinEthBalance;
  12. return meta.getBalance.call(accounts[0]).then(function(outCoinBalance){
  13. metaCoinBalance = outCoinBalance.toNumber();
  14. return meta.getBalanceInEth.call(accounts[0]);
  15. }).then(function(outCoinBalanceEth){
  16. metaCoinEthBalance = outCoinBalanceEth.toNumber();
  17. }).then(function(){
  18. assert.equal(metaCoinEthBalance,2*metaCoinBalance,"Library function returned unexpeced function, linkage may be broken");
  19. });
  20. });
  21. it("should send coin correctly", function() {
  22. var meta = MetaCoin.deployed();
  23. // Get initial balances of first and second account.
  24. var account_one = accounts[0];
  25. var account_two = accounts[1];
  26. var account_one_starting_balance;
  27. var account_two_starting_balance;
  28. var account_one_ending_balance;
  29. var account_two_ending_balance;
  30. var amount = 10;
  31. return meta.getBalance.call(account_one).then(function(balance) {
  32. account_one_starting_balance = balance.toNumber();
  33. return meta.getBalance.call(account_two);
  34. }).then(function(balance) {
  35. account_two_starting_balance = balance.toNumber();
  36. return meta.sendCoin(account_two, amount, {from: account_one});
  37. }).then(function() {
  38. return meta.getBalance.call(account_one);
  39. }).then(function(balance) {
  40. account_one_ending_balance = balance.toNumber();
  41. return meta.getBalance.call(account_two);
  42. }).then(function(balance) {
  43. account_two_ending_balance = balance.toNumber();
  44. assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender");
  45. assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver");
  46. });
  47. });
  48. });