服务端阅读 02月21日 15:59
如何在 Hardhat 中编写智能合约测试?
Hardhat 提供了强大的测试框架,基于 Mocha 和 Chai,以下是编写测试的核心要点:测试文件结构:const { expect } = require("chai");const { ethers } = require("hardhat");describe("MyContract", function () { beforeEach(async function () { const MyContract = await ethers.getContractFactory("MyContract"); this.contract = await MyContract.deploy(); await this.contract.deployed(); }); it("should return the correct value", async function () { expect(await this.contract.getValue()).to.equal(42); });});核心功能:合约部署const Contract = await ethers.getContractFactory("ContractName");const contract = await Contract.deploy();await contract.deployed();函数调用和断言const tx = await contract.someFunction(param1, param2);await tx.wait(); // 等待交易确认expect(await contract.someViewFunction()).to.equal(expectedValue);事件监听await expect(contract.someFunction()) .to.emit(contract, "EventName") .withArgs(arg1, arg2);交易回滚测试await expect(contract.failingFunction()) .to.be.revertedWith("Error message");快照功能const snapshot = await ethers.provider.send("evm_snapshot", []);// 执行一些操作await ethers.provider.send("evm_revert", [snapshot]);时间操作await ethers.provider.send("evm_increaseTime", [3600]); // 增加1小时await ethers.provider.send("evm_mine"); // 挖掘新区块最佳实践:使用 beforeEach 和 afterEach 管理测试状态为每个测试用例提供清晰的描述测试正常路径和异常路径使用有意义的测试数据保持测试的独立性和可重复性