【区块链】truffle测试

配置区块链网络

启动Ganache软件

使用VScode打开项目的wordspace

配置对外访问的RPC接口为7545,配置项目的truffle-config.js实现与新建Workspace的连接。

创建项目

创建一个新的目录

bash 复制代码
mkdir MetaCoin
cd MetaCoin

下载metacoin盒子

bash 复制代码
truffle unbox metacoin
bash 复制代码
contracts/:Solidity 合约目录
migrations/:可编写脚本的部署文件的目录
test/:用于测试应用程序和合约的测试文件目录
truffle-config.js:松露配置文件

编译合约

bash 复制代码
truffle compile --all

编写测试用例

基本用法

artifacts.require()

引入合约的名称,返回抽象合约

javascript 复制代码
const HelloWorld = artifacts.require("HelloWorld");
module.exports

导出函数

javascript 复制代码
module.exports = function(deployer, network, accounts) {
  
}

// Deploy a single contract without constructor arguments
deployer.deploy(A);

// Deploy a single contract with constructor arguments
deployer.deploy(A, arg1, arg2, ...);

// Don't deploy this contract if it has already been deployed
deployer.deploy(A, {overwrite: false});
contract()
  • 在函数运行之前,合约将被重新部署到正在运行的以太坊客户端,以便其中的测试以干净的合约状态运行。
  • 提供以太坊客户端可用的帐户列表,您可以使用这些帐户来编写测试。
it()

每一个需要执行的测试用例。

相关推荐
好多大米15 分钟前
W2D3-Foundry 测试
区块链·solidity
Bczheng18 小时前
四.比特币默克尔树(上)
区块链
OxYGC9 小时前
[Web3] 一文读懂区块链中的账本类型
web3·区块链
david_lv10 小时前
大A,2026年Q1总结
区块链
筱璦10 小时前
期货软件开发 - 策略编辑
前端·区块链·交易·期货
Risk Actuary1 天前
侧挂车(Sidecar)与巨灾债券(Cat Bond)
区块链
Css38RttP1 天前
springMVC-RequestMapping注解
区块链
Amos_Web1 天前
Solana开发(1)- 核心概念扫盲篇&&扫雷篇
前端·rust·区块链
OPHKVPS2 天前
GoBruteforcer(GoBrut)僵尸网络新攻势:AI 生成弱配置成“帮凶”,瞄准加密货币及区块链数据库
网络·人工智能·区块链
好家伙VCC2 天前
**发散创新:基于以太坊侧链的高性能去中心化应用部署实战**在区块链生态中,*
java·python·去中心化·区块链