• 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)

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

  • 相关阅读:
    Oracle Primavera P6V7 SQL异常案例
    真正“搞”懂HTTP协议03之时间穿梭
    Springmvc笔记
    Elasticsearch下载
    更改主机名的方法(永久)
    【二】async和await_vue环境和工具
    艾美捷曲妥珠单抗Trastuzumab参数和相关研究
    ActiveMQ 反序列化漏洞(CVE-2015-5254)特征分析
    ResizeObserver观察元素宽度的变化
    曾遭作者“删库”的faker.js,现被社区接手;Apache Ambari 项目被弃用;FFmpeg 5.0 发布 | 开源日报
  • 原文地址:https://blog.csdn.net/2302_77339802/article/details/134451494