Index

Solidity

  1. types
  2. External
  3. Library
    1. self in Library
    2. using * for
    3. self example
  4. interfaces
  5. import
  6. msg.value
  7. Handling ether
  8. Erc20 token in contract
  9. assembly
  10. Enum
  11. memory, storage, calldata
  12. Array Pushing
  13. length in array
  14. Struct array
  15. mapping
  16. bytes and string
  17. uint to bytes32
  18. returns with name
  19. score voting
  20. Tests
    1. Async Tests
    2. Hooks

Async Tests

const DappTokenSale = artifacts.require("DappTokenSale")
const DappToken = artifacts.require("DappToken")

contract("DappToken", async accounts => {
  let admin
  let buyer
  let tokenPrice = 1000000000000000 //wei (0.001 ether)
  // 3. Set the token available for sale
  let tokensAvailable = 750000
  let numberOfTokens
  it("Token buying", async () => {
    admin = accounts[0]
    buyer = accounts[1]
    console.log(admin, "Admin")
    console.log(buyer, "buyer")

    let dapptokenInstance = await DappToken.deployed()
    let dapptokenSale = await DappTokenSale.deployed()
    console.log(dapptokenInstance.address, "Token address")
    console.log(dapptokenSale.address, "Token Sale address")
  })
})




const MetaCoin = artifacts.require("MetaCoin");

contract("2nd MetaCoin test", async accounts => {
  it("should put 10000 MetaCoin in the first account", async () => {
    let instance = await MetaCoin.deployed();
    let balance = await instance.getBalance.call(accounts[0]);
    assert.equal(balance.valueOf(), 10000);
  });

  it("should call a function that depends on a linked library", async () => {
    let meta = await MetaCoin.deployed();
    let outCoinBalance = await meta.getBalance.call(accounts[0]);
    let metaCoinBalance = outCoinBalance.toNumber();
    let outCoinBalanceEth = await meta.getBalanceInEth.call(accounts[0]);
    let metaCoinEthBalance = outCoinBalanceEth.toNumber();
    assert.equal(metaCoinEthBalance, 2 * metaCoinBalance);
  });

  it("should send coin correctly", async () => {
    // Get initial balances of first and second account.
    let account_one = accounts[0];
    let account_two = accounts[1];

    let amount = 10;

    let instance = await MetaCoin.deployed();
    let meta = instance;

    let balance = await meta.getBalance.call(account_one);
    let account_one_starting_balance = balance.toNumber();

    balance = await meta.getBalance.call(account_two);
    let account_two_starting_balance = balance.toNumber();
    await meta.sendCoin(account_two, amount, { from: account_one });

    balance = await meta.getBalance.call(account_one);
    let account_one_ending_balance = balance.toNumber();

    balance = await meta.getBalance.call(account_two);
    let account_two_ending_balance = balance.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"
    );
  });
});


truffle test ./test/metacoin.js