cryptopunks測試代碼cryptopunksmarket-setinitial.js

require('babel-polyfill');
//測試用例要在執行完了truffle compile和truffle migrate後才能使用truffle test來進行測試
//要注意artifacts只有在truffle框架裏能使用
var CryptoPunksMarket = artifacts.require("./CryptoPunksMarket.sol");

//要注意contract只有在truffle框架裏能使用,mocha中使用describe塊開始
contract('CryptoPunksMarket-setInitial', function (accounts) {
//it爲測試用例,是同步進行訪問的,前面的string是用來解釋該測試用例的測試目的是什麼
  it("Should start with 0 balance", async function () {
    //聲明使用的是truffle migrate部署好後的CryptoPunksMarket智能合約
    var contract = await CryptoPunksMarket.deployed();

    //而後調用裏面的setInitialOwner初始化用戶函數,將punk0的擁有者設爲accounts[0]
    await contract.setInitialOwner(accounts[0], 0);
    //而後經過查看獲得用戶accounts[0]擁有的token數量
    var balance = await contract.balanceOf.call(accounts[0]);
    //這就是chai的斷言部分,查看balance.valueOf()是否與1相等,若是是,則繼續向下運
    //行,若是不成功將會輸出錯誤信息Didn't get the initial punk,並且該測試用例it回結
    //束,最後會標明出錯
    assert.equal(balance.valueOf(), 1, "Didn't get the initial punk");
    //查看punkIndex爲0的擁有者是誰
    var owner = await contract.punkIndexToAddress.call(0);
    //擁有者應該是accounts[0],不然報錯
    assert.equal(owner, accounts[0], "Ownership array wrong");
    //初始化punk有10000個,分配一個給accounts[0]後,應該還剩9999個
    var remaining = await contract.punksRemainingToAssign.call();
    assert.equal(9999, remaining);

    // todo Set this back to 10000 for final runs
    var assignCoins = 100;
    //給用戶accounts[0]分配99個punk,包括以前那個,應該有100個
    for (var i=1; i<assignCoins; i++) {
      await contract.setInitialOwner(accounts[0], i);
    }
    //因此如今剩餘的punk有10000-100個
    var remainingAfter = await contract.punksRemainingToAssign.call();
    assert.equal(10000-assignCoins, remainingAfter);
  
    var balanceAfter = await contract.balanceOf.call(accounts[0]);
    assert.equal(assignCoins, balanceAfter);

  }),
    //該用例也是用來分配punk的
    it("bulk assign", async function () {
      //該聲明使用的合約與上面的測試用例是同樣的,可是這裏我會碰見一個問題,就是根據
     //這個函數運行的邏輯來看,該it是在上面的it結束後才運行的,這樣才能保證此時被分配
     //的punk有110個,咱們truffle test運行起來後發現的確是成功的,可是若是咱們沒有
     //truffle的框架中運行的話,it實際上是會同步運行的,因此我想多是truffle在包裝的時候
     //進行了設置,因此它沒有使用describe測試套件,而是使用了自定義的contract,這是我
     //還存疑的地方
      var contract = await CryptoPunksMarket.deployed();
      var owners = [accounts[0], accounts[1], accounts[2], accounts[3], accounts[4], accounts[5], accounts[6], accounts[7], accounts[8], accounts[9]];
      var punks = [1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009];
      await contract.setInitialOwners(owners, punks);
      for (var i = 0; i < 10; i++) {
        var currentOwner = await contract.punkIndexToAddress.call(punks[i]);
        assert.equal(currentOwner, owners[i]);
      }
      var remainingAfter = await contract.punksRemainingToAssign.call();
      assert.equal(10000-110, remainingAfter);
    }),
    it("can not pass an invalid index to assign initial", async function () {
      var contract = await CryptoPunksMarket.deployed();
      try {
        await contract.setInitialOwner(accounts[0], 10000);
        assert(false, "Should have thrown exception.");
      } catch (err) {
        // Should catch an exception
      }

    }),
    it("only owner can assign initial", async function () {
      var contract = await CryptoPunksMarket.deployed();
      try {
        await contract.setInitialOwner(accounts[1], 1);
        assert(false, "Should have thrown exception.");
      } catch (err) {
        // Should catch an exception
      }

    }),
    it("Can not claim punk after set initial owners assigned", async function () {
      var contract = await CryptoPunksMarket.deployed();
      await contract.allInitialOwnersAssigned();
      try {
        await contract.setInitialOwner(accounts[0], 0);
        assert(false, "Should have thrown exception.");
      } catch (err) {
        // Should catch an exception
      }

    })
});

上面這個就是測試代碼cryptopunksmarket-setinitial.js及其一些解釋babel

相關文章
相關標籤/搜索