• FISCOBCOS入门(十)Truffle测试helloworld智能合约


    在windos终端内安装truffle

    npm install -g truffle

    truffle --version

    出现上图情况也没问题

    下面就可以进行我们的操作了

    创建一个文件truffle

    创建一个空工程

    truffle init

    在contracts内加入HelloWorld合约

    1. // SPDX-License-Identifier: MIT
    2. pragma solidity ^0.8.0;
    3. contract HelloWorld {
    4. string private greeting;
    5. // 构造函数,设置初始问候语
    6. constructor() {
    7. greeting = "Hello World";
    8. }
    9. // 获取当前的问候语
    10. function get() public view returns (string memory) {
    11. return greeting;
    12. }
    13. // 设置新的问候语
    14. function set(string memory newGreeting) public {
    15. greeting = newGreeting;
    16. }
    17. }

    在migration内编写迁移脚本

    1. const HelloWorld = artifacts.require("HelloWorld");
    2. module.exports = async function (deployer) {
    3. // 部署 HelloWorld 合约
    4. await deployer.deploy(HelloWorld);
    5. };

    在test文件内添加测试脚本

    1. const Helloworld = artifacts.require("HelloWorld");
    2. contract("HelloWorld", async() => {
    3. let newData="123";
    4. it("Test helloWorld get", async () => {
    5. const hello = await Helloworld.deployed();
    6. let name = await hello.get();
    7. console.log("init data",name);
    8. assert.equal(name,"Hello World","Test fail");
    9. });
    10. it("Test helloWorld set", async () => {
    11. const hello = await Helloworld.deployed();
    12. await hello.set(newData);
    13. });
    14. it("Test HelloWorld get2", async () => {
    15. const hello = await Helloworld.deployed();
    16. let name = await hello.get();
    17. console.log("Updated data:", name);
    18. assert.equal(name,"123","Test fail");
    19. });
    20. });

    更改文件truffle-config.js

    文件总览

    开始正式测试,测试的顺序为:truffle develop(启用测试网络) => compile(编译合约) => migrate(部署合约) => test(测试合约)

    部署

    truffle develop

    compile

    migrate

    测试(truffle test)

    测试成功,别看流程这么简单,中间解决好多报错,用用一键三连,学习更多查看我的其它博客

  • 相关阅读:
    VUE局部修改ElementUI样式
    armlinux 移植qt
    HBase数据存储
    tiup cluster enable
    C++ 顺序线性表的功能
    Docker学习-目录
    如何在k8s的Java服务镜像(Linux)中设置中文字体
    在腾讯云安装docker及zookeeper和dubbo
    Flask模板_循环结构
    什么是软件测试?
  • 原文地址:https://blog.csdn.net/2302_77339802/article/details/134451494