npm install -g truffle
truffle --version
出现上图情况也没问题
下面就可以进行我们的操作了
truffle init
- // SPDX-License-Identifier: MIT
- pragma solidity ^0.8.0;
- contract HelloWorld {
- string private greeting;
- // 构造函数,设置初始问候语
- constructor() {
- greeting = "Hello World";
- }
-
- // 获取当前的问候语
- function get() public view returns (string memory) {
- return greeting;
- }
-
- // 设置新的问候语
- function set(string memory newGreeting) public {
- greeting = newGreeting;
- }
-
- }
- const HelloWorld = artifacts.require("HelloWorld");
-
- module.exports = async function (deployer) {
- // 部署 HelloWorld 合约
- await deployer.deploy(HelloWorld);
- };
- const Helloworld = artifacts.require("HelloWorld");
- contract("HelloWorld", async() => {
- let newData="123";
-
- it("Test helloWorld get", async () => {
- const hello = await Helloworld.deployed();
- let name = await hello.get();
- console.log("init data",name);
- assert.equal(name,"Hello World","Test fail");
- });
-
- it("Test helloWorld set", async () => {
- const hello = await Helloworld.deployed();
-
-
- await hello.set(newData);
-
-
- });
-
- it("Test HelloWorld get2", async () => {
-
- const hello = await Helloworld.deployed();
- let name = await hello.get();
-
- console.log("Updated data:", name);
- assert.equal(name,"123","Test fail");
- });
-
- });
更改文件truffle-config.js
文件总览
开始正式测试,测试的顺序为:truffle develop(启用测试网络) => compile(编译合约) => migrate(部署合约) => test(测试合约)
truffle develop
compile
migrate
测试成功,别看流程这么简单,中间解决好多报错,用用一键三连,学习更多查看我的其它博客