首页 > 其他分享 >9.区块链系列之hardhat框架测试合约

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

时间:2022-10-29 15:56:50浏览次数:130  
标签:const 框架 simpleStorage await ---------- hardhat 100 区块

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

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


  SimpleStorage
    ✔ Should start with a favorite number of 0
    ✔ Should 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

欢迎关注公众号算法小生沈健的技术博客查看最新文章

标签:const,框架,simpleStorage,await,----------,hardhat,100,区块
From: https://www.cnblogs.com/shenjian-online/p/16838904.html

相关文章

  • 8.区块链系列之hardhat框架部署合约(二)
    现在我们来实践hardhat部署合约中的其他更多技术要点1.代码方式验证合约注册https://etherscan.io/,如下图添加拷贝API_KEY在.env文件中新增ETHERSCAN_API_KEYE......
  • 10.区块链系列之hardhat部署抵押赎回Fund合约
    本文继续通过笔者学习到的抵押赎回智能合约Fund来进一步学习solidity语言,加深对开发的理解,其中通过storage节省gas是需要重点实践的,毕竟涉及到资产代码已提交至https://gi......
  • GF(Go Frame)开发框架
    ​​GF(GoFrame)​​是一款模块化、松耦合、生产级的Go应用开发框架。提供了常用的核心开发组件,如:缓存、日志、文件、时间、队列、数组、集合、字符串、定时器、命令行、文......
  • robotframework自动化测试框架实战教程:库文档工具(Libdoc)
    Libdoc是RobotFramework内置的工具,用来为测试库和资源文件生成关键字的文档.文档分为HTML和XML格式,前者供人阅读,后者供RIDE和其它工具使用. 文档文件被指定为库/......
  • #打卡不停更#【FFH】浅析Ability框架中Stage模型与FA模型的差异
    (#打卡不停更#【FFH】浅析Ability框架中Stage模型与FA模型的差异)Aility框架概述Ability是应用所具备能力的抽象,也是应用程序的基本组成单元。OpenHarmony与HarmonyOS的应......
  • Model/View框架简介-Qt
    一.Model/View框架简介1.Model/View框架核心思想    Model/View框架的核心思想是模型(数据)与视图(显示)相分离,模型对外提供标准接口存取数据,不关心数据如何显示,视图自定......
  • 分布式定时任务设计及其框架
    在很多应用系统中,我们常常要定时或周期性执行一些任务。比如,订单系统的超时状态判断、缓存数据的定时更新、定时给用户发邮件,甚至是一些定期计算的报表等。单机程序中常见的......
  • vs2015中搭建EF框架在代码生成策略中找不到“旧的ObjectContext
    vs2015中搭建EF框架在代码生成策略中找不到“旧的ObjectContext”生生不息0.o于2022-01-0617:28:10发布429收藏分类专栏:C#EF文章标签:c#软件框架版权C#同时被......
  • 精通区块链编程
    2比特币的工作原理2.1交易、区块、挖矿和区块链2.1.1比特币概述比特币系统是由用户,交易和矿工三方组成的。用户使用包含密钥的钱包,交易在整个网络传播,而矿工则生产出......
  • SpringCloud微服务实战——搭建企业级开发框架(四十八):【移动开发】整合uni-app搭建移动
      uni-app默认使用uni-ui全端兼容的、高性能UI框架,在我们开发过程中可以满足大部分的需求了,并且如果是为了兼容性,还是强烈建议使用uni-ui作为UI框架使用。  如果作为......