利用mocha進行以太坊智慧合約編譯部署測試

語言: CN / TW / HK

    使用智慧合約程式語言solidity編寫的智慧合約,除了可以直接通過以太坊的工具鏈truffle,ganache-cli進行測試之外,還可以結合mocha進行單元測試。

     mocha單元測試本質上,還是需要對合約進行編譯、部署,只不過可以通過程式碼的形式進行直觀的操作,而不是通過truffle命令來進行編譯、部署、測試。

    首先,構建工程,我們可以根據構建node專案的方式構建:

    新增依賴:package.json

"dependencies": {
    "ganache-cli": "^6.12.2",
    "mocha": "^8.2.1",
    "solc": "^0.4.26",
    "web3": "^1.3.3"
}

    專案結構這裡簡單遵循以太坊專案的結構建立一個contracts資料夾,用來儲存合約。然後在contracts目錄下新建HelloWorld.sol

pragma solidity ^0.4.23;

contract HelloWorld{
  string public name;
  constructor(string _name) public{
    name = _name;
  }
  function getName() public view returns(string){
    return name;
  }
  function changeName(string _name) public {
    name = _name;
  }
}

    編寫compile.js用來編譯合約:

const path = require('path')
const fs = require('fs')
const solc = require('solc')
const filepath = path.resolve(__dirname,'contracts','HelloWorld.sol')
const source = fs.readFileSync(filepath,'utf8')
module.exports = solc.compile(source,1).contracts[":HelloWorld"]

    建立test資料夾,用來儲存測試程式碼,編寫mocha測試程式碼:helloworld.test.js

const ganache = require('ganache-cli')
const Web3 = require('web3')
const assert = require('assert')
const web3 = new Web3(ganache.provider())

const {bytecode,interface} = require('../compile')

var helloworld;
var fetchAccounts;
beforeEach(async ()=>{
    /*
    web3.eth.getAccounts().then(fetchAccounts=>{
        console.log(fetchAccounts)
    })*/
    fetchAccounts = await web3.eth.getAccounts()
    helloworld = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({data:bytecode,arguments:['abc']})
    .send({from:fetchAccounts[0],gas:'1000000'})
})

describe('HelloWorld',()=>{
    it('deploy contract',()=>{
        assert.ok(helloworld.options.address)
    })

    it('call static function',async ()=>{
        const message = await helloworld.methods.getName().call()
        assert.equal('abc',message)
    })

    it('call dynamic function',async ()=>{
        await helloworld.methods.changeName('xyz').send({from:fetchAccounts[0]})
        const message = await helloworld.methods.getName().call()
        assert.equal('xyz',message)
    })
})

    程式碼準備完畢,我們可以在package.json中配置測試scripts選項:

     

   之後,在命令列下執行單元測試:npm test

    

    單元測試全部通過,表示智慧合約編譯部署測試均正常,我們在進行測試的時候,傳入了很多引數,合約部署之後,每一次呼叫,都需要進行真實的交易,所以需要賬戶資訊,需要轉賬操作,這裡面有進行靜態方法呼叫,也有動態方法呼叫,因為智慧合約編譯之後,函式呼叫都是非同步操作,所以使用了sync await來非同步轉同步,進而獲取呼叫結果。

    以上程式碼全部參考知乎系列影片全棧react、nodejs結合區塊鏈專案而來,有興趣的可以從此進入:http://www.zhihu.com/people/ke-ai-de-xiao-tu-ji-71/zvideos?page=3