truffle测试步骤流程

1、安装Ganache

安装本地Ganache 进入官网:Ganache - Truffle SuiteQuickly fire up a personal Ethereum blockchain which you can use to run tests, execute commands, and inspect state while controlling how the chain operates.icon-default.png?t=O83Ahttps://trufflesuite.com/ganache/我们下载对应的电脑系统的版本,下载安装包后,直接安装即可

按照进入ganache有两个选项,点击NEW WORKAPACE新建

我们选择定制启动,可以配置我们的一个PORT NUMBET,NETWORK ID等等

2、部署

下载node.js 利用npm 下载truffle :

npm install -g truffle

(在vs code新建truffle项目)

 打开新建的项目,打开终端输入 truffle init

3、接下来我们来将下面的HelloWorld合约放到我们工程中的contracts中

pragma solidity ^0.5.2;

contract HelloWorld {  

string name;

event setEvent (

      string name

   );

constructor() public {      

name = "Hello, World!";

}

function get() public view returns (string memory) {

       return name;

   }

function set(string memory n) public {

       name = n;

       emit setEvent(n);

   }

}

4、编写部署脚本(多个迁移文件,开头必须加数字,表明迁移顺序) 

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

module.exports = async function (deployer) {

    await deployer.deploy(HelloWorld);

    const h = await HelloWorld.deployed();

    console.log("部署第一脚本地址="+h.address);

}

5、修改truffle-config.js 配置, 下面的port对应也是本地Ganche的配置

6、修改truffle-config.js 配置对应合约的版本(注意:合约版本得对应,否则会出错)

7、在终端输入 truffle compile编译

8、在终端输入truffle migrate部署

9、部署好之后,就回去看ganache,如果TX COUNT变成了1,就说明成功了。