9.区块链系列之hardhat框架测试合约

先前我们讲解了如何部署智能合约,今天我们来对合约进行测试,这是非常重要的一部分,毕竟一旦部署后不可变,如果测试不充分,那么黑客就不会客气了

1. 单元测试

在test目录下新建test-deploy.js

const { ethers } = require("hardhat")
const { expect, assert } = require("chai")

describe("SimpleStorage", function () {
  let simpleStorageFactory, simpleStorage
  beforeEach(async function () {
    simpleStorageFactory = await ethers.getContractFactory("SimpleStorage")
    simpleStorage = await simpleStorageFactory.deploy()
  })

  it("Should start with a favorite number of 0", async function () {
    const currentValue = await simpleStorage.retrieve()
    const expectedValue = "0"
    assert.equal(currentValue.toString(), expectedValue)
  })
  
  it("Should update when we call store", async function () {
    const expectedValue = "7"
    const transactionResponse = await simpleStorage.store(expectedValue)
    await transactionResponse.wait(1)

    const currentValue = await simpleStorage.retrieve()
    assert.equal(currentValue.toString(), expectedValue)
  })

  it("Should work correctly with the people struct and array", async function () {
    const expectedPersonName = "shenjian"
    const expectedFavoriteNumber = "6"
    const transactionResponse = await simpleStorage.addPerson(
      expectedPersonName,
      expectedFavoriteNumber
    )
    await transactionResponse.wait(1)
    const { favoriteNumber, name } = await simpleStorage.people(0)
    assert.equal(name, expectedPersonName)
    assert.equal(favoriteNumber, expectedFavoriteNumber)
  })
})

运行yarn hardhat test

(base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat test
yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat test


  SimpleStorageShould start with a favorite number of 0Should update when we call store
    ✔ Should work correctly with the people struct and array (46ms)


  3 passing (3s)

Done in 4.42s.
2. GAS报告
  • 注册https://pro.coinmarketcap.com/以获得API_KEY

  • 在.env文件中添加配置

COINMARKETCAP_API_KEY=刚才注册的API_KEY
  • 在hardhat中新增以下代码
const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY

module.exports = {
  ......
  gasReporter: {
    enabled: true,
    currency: "USD",
    outputFile: "gas-report.txt",
    noColors: true,
    coinmarketcap: COINMARKETCAP_API_KEY
  },
}
  • 运行yarn hardhat test生成gas-report.txt
·-------------------------------|----------------------------|-------------|-----------------------------·
|      Solc version: 0.8.8      ·  Optimizer enabled: false  ·  Runs: 200  ·  Block limit: 30000000 gas  │
································|····························|·············|······························
|  Methods                                                                                               │
··················|·············|··············|·············|·············|···············|··············
|  Contract       ·  Method     ·  Min         ·  Max        ·  Avg        ·  # calls      ·  usd (avg)  │
··················|·············|··············|·············|·············|···············|··············
|  SimpleStorage  ·  addPerson  ·           -  ·          -  ·     112419  ·            2  ·          -  │
··················|·············|··············|·············|·············|···············|··············
|  SimpleStorage  ·  store      ·           -  ·          -  ·      43724  ·            2  ·          -  │
··················|·············|··············|·············|·············|···············|··············
|  Deployments                  ·                                          ·  % of limit   ·             │
································|··············|·············|·············|···············|··············
|  SimpleStorage                ·           -  ·          -  ·     463670  ·        1.5 %  ·          -  │
·-------------------------------|--------------|-------------|-------------|---------------|-------------·
3. 测试代码覆盖率查看

运行yarn hardhat coverage命令

--------------------|----------|----------|----------|----------|----------------|
File                |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
--------------------|----------|----------|----------|----------|----------------|
 contracts\         |      100 |      100 |      100 |      100 |                |
  SimpleStorage.sol |      100 |      100 |      100 |      100 |                |
--------------------|----------|----------|----------|----------|----------------|
All files           |      100 |      100 |      100 |      100 |                |
--------------------|----------|----------|----------|----------|----------------|

所有代码已上传至https://gitee.com/SJshenjian/blockchain/tree/master/hardhat-simple-storage-fcc

欢迎关注公众号算法小生或沈健的技术博客shenjian.online

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沈健_算法小生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值