我们先看一个简单的测试例子,然后拆开来讲每一行
const { expect } = require("chai");
describe("Token contract", function() {
it("Deployment should assign the total supply of tokens to the owner", async function() {
const [owner] = await ethers.getSigners();
const Token = await ethers.getContractFactory("Token");
const hardhatToken = await Token.deploy();
const ownerBalance = await hardhatToken.balanceOf(owner.address);
expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
});
});
先解释第一行,如果没有前段基础会看的很懵逼 。
此处我给一个前端的例子参考一下 :
// CommonJS模块
let { stat, exists, readFile } = require('fs');
// 等同于
let _fs = require('fs');
let stat = _fs.stat;
let exists = _fs.exists;
let readfile = _fs.readfile;
后面的第二行 都是固定写法 包括 describe ,it 等关键字
这里我们在说一个例子
例如,如果我们要测试此功能:

本文通过实例讲解如何使用Ethers.js进行Token contract的部署,并测试是否正确地将初始供应量分配给了合约创建者。涉及Signer对象、ContractFactory的使用和balanceOf方法的验证。
最低0.47元/天 解锁文章
3571

被折叠的 条评论
为什么被折叠?



