利用mocha进行以太坊智能合约编译部署测试

红太狼 2023-01-11 03:52 231阅读 0赞
  1. 使用智能合约编程语言solidity编写的智能合约,除了可以直接通过以太坊的工具链truffle,ganache-cli进行测试之外,还可以结合mocha进行单元测试。
  2. mocha单元测试本质上,还是需要对合约进行编译、部署,只不过可以通过代码的形式进行直观的操作,而不是通过truffle命令来进行编译、部署、测试。
  3. 首先,构建工程,我们可以根据构建node项目的方式构建:
  4. 添加依赖:package.json
  5. "dependencies": {
  6. "ganache-cli": "^6.12.2",
  7. "mocha": "^8.2.1",
  8. "solc": "^0.4.26",
  9. "web3": "^1.3.3"
  10. }
  11. 项目结构这里简单遵循以太坊项目的结构建立一个contracts文件夹,用来保存合约。然后在contracts目录下新建HelloWorld.sol
  12. pragma solidity ^0.4.23;
  13. contract HelloWorld{
  14. string public name;
  15. constructor(string _name) public{
  16. name = _name;
  17. }
  18. function getName() public view returns(string){
  19. return name;
  20. }
  21. function changeName(string _name) public {
  22. name = _name;
  23. }
  24. }
  25. 编写compile.js用来编译合约:
  26. const path = require('path')
  27. const fs = require('fs')
  28. const solc = require('solc')
  29. const filepath = path.resolve(__dirname,'contracts','HelloWorld.sol')
  30. const source = fs.readFileSync(filepath,'utf8')
  31. module.exports = solc.compile(source,1).contracts[":HelloWorld"]
  32. 建立test文件夹,用来保存测试代码,编写mocha测试代码:helloworld.test.js
  33. const ganache = require('ganache-cli')
  34. const Web3 = require('web3')
  35. const assert = require('assert')
  36. const web3 = new Web3(ganache.provider())
  37. const {bytecode,interface} = require('../compile')
  38. var helloworld;
  39. var fetchAccounts;
  40. beforeEach(async ()=>{
  41. /*
  42. web3.eth.getAccounts().then(fetchAccounts=>{
  43. console.log(fetchAccounts)
  44. })*/
  45. fetchAccounts = await web3.eth.getAccounts()
  46. helloworld = await new web3.eth.Contract(JSON.parse(interface))
  47. .deploy({data:bytecode,arguments:['abc']})
  48. .send({from:fetchAccounts[0],gas:'1000000'})
  49. })
  50. describe('HelloWorld',()=>{
  51. it('deploy contract',()=>{
  52. assert.ok(helloworld.options.address)
  53. })
  54. it('call static function',async ()=>{
  55. const message = await helloworld.methods.getName().call()
  56. assert.equal('abc',message)
  57. })
  58. it('call dynamic function',async ()=>{
  59. await helloworld.methods.changeName('xyz').send({from:fetchAccounts[0]})
  60. const message = await helloworld.methods.getName().call()
  61. assert.equal('xyz',message)
  62. })
  63. })
  64. 代码准备完毕,我们可以在package.json中配置测试scripts选项:
  65. ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZlaW5pZmk_size_16_color_FFFFFF_t_70][]

之后,在命令行下运行单元测试:npm test

  1. ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZlaW5pZmk_size_16_color_FFFFFF_t_70 1][]
  2. 单元测试全部通过,表示智能合约编译部署测试均正常,我们在进行测试的时候,传入了很多参数,合约部署之后,每一次调用,都需要进行真实的交易,所以需要账户信息,需要转账操作,这里面有进行静态方法调用,也有动态方法调用,因为智能合约编译之后,函数调用都是异步操作,所以使用了sync await来异步转同步,进而获取调用结果。
  3. 以上代码全部参考知乎系列视频全栈reactnodejs结合区块链项目而来,有兴趣的可以从此进入:[https://www.zhihu.com/people/ke-ai-de-xiao-tu-ji-71/zvideos?page=3][https_www.zhihu.com_people_ke-ai-de-xiao-tu-ji-71_zvideos_page_3]

发表评论

表情:
评论列表 (有 0 条评论,231人围观)

还没有评论,来说两句吧...

相关阅读

    相关 智能合约 简介

    以太坊是一个分布式的计算平台。它会生成一个名为Ether的加密货币。程序员可以在以太坊区块链上写下“智能合约”,这些以太坊智能合约会根据代码自动执行。 以太坊是什么?

    相关 智能合约是什么?

    以太坊是最早提出做智能合约的平台。由于以太坊区块链被普遍接受,因此多数区块链的智能合约采取与以太坊相似的设计。本文将详细介绍以太坊的智能合约:它是什么?它有什么用? 以太...