Home >Technology peripherals >It Industry >Truffle: Testing Smart Contracts

Truffle: Testing Smart Contracts

Christopher Nolan
Christopher NolanOriginal
2025-02-16 09:14:13963browse

Truffle: Testing Smart Contracts

Truffle: A powerful tool for efficient automation of smart contract development. This article focuses on smart contract testing.

Smart contract testing is the core link of high-quality smart contract development. Why do we need to pay so much attention to testing? Because smart contracts deal with value, sometimes huge value, this makes them targeted by attackers. You don't want to see your project eventually become a "ghost" in the blockchain cemetery, right?

Key points:

  • Truffle is a must-have tool for automated compilation, testing and deployment of smart contracts, ensuring efficient blockchain development.
  • Because smart contracts handle high-value transactions and are easily targeted, it is crucial to test smart contracts with Truffle.
  • Easy to build a local development test network using ganache-cli (formerly known as TestRPC), which provides pre-loaded Ether coins for cost-effective testing.
  • Truffle supports writing smart contract tests using Solidity and JavaScript, providing flexibility based on developer programming preferences and test complexity.
  • Truffle has a built-in debugger that allows developers to step through code and check variables to solve problems efficiently.

Beginner:

We will create a simple smart contract-based second-hand goods market called HashMarket.

In the terminal, locate the folder where you want to build the project. In this folder, run the following command:

<code class="language-bash">mkdir HashMarket
cd HashMarket
truffle init</code>

You should see output similar to the following:

<code>Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!

Commands:

  Compile:        truffle compile
  Migrate:        truffle migrate
  Test contracts: truffle test</code>

You will also get the file structure as shown below:

<code>.
├── contracts
│   └── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
├── test
├── truffle-config.js
└── truffle.js</code>

For these documents, please refer to the previous article. In short, we have the basic truffle.js files as well as two files for the initial blockchain migration.

Prepare for the test environment

The easiest way to test is to do it on the local network. It is highly recommended to use the ganache-cli (formerly known as TestRPC) tool for contract testing.

Installing ganache-cli (requires Node package manager):

<code class="language-bash">npm install -g ganache-cli</code>
After

, open a separate terminal window or tab and run:

<code class="language-bash">ganache-cli</code>

You should see output similar to the following:

<code>Ganache CLI v6.1.0 (ganache-core: 2.1.0)

... (账户和私钥信息) ...

Listening on localhost:8545</code>

This is a list of all accounts that ganache-cli creates for you. You can use any account you want, but these will preload ether, which makes them very useful (because the test requires ether to pay for gas fees).

After

, go to your truffle.js or truffle-config.js file and add a development network to your configuration:

<code class="language-javascript">module.exports = {
    networks: {
      development: {
        host: "127.0.0.1",
        port: 8545,
        network_id: "*"
      }
    }
};</code>

Writing smart contracts

First, we will write a HashMarket smart contract. We will try to keep it simple while retaining the required functionality.

HashMarket is eBay on the blockchain. It allows sellers to publish products and buyers to purchase products using Ether. It also allows sellers to remove the product when it is not sold.

In the contracts folder in the project, create a new file and name it HashMarket.sol. In this file, add the following code: (The same contract code as the original text is omitted here to avoid duplication)

Writing a migration file

You need to write a migration file that lets Truffle know how to deploy your contract to the blockchain. Go to the migrations folder and create a new file named 2_deploy_contracts.js. In this file, add the following code: (The same migration file code as the original text is omitted here to avoid duplication)

Test smart contracts

You can use Solidity or JavaScript to perform smart contract testing. Solidity may be a bit more intuitive when testing smart contracts, but JavaScript offers more possibilities.

Solidity Test

To start the test, in the test folder in the project, create a file named TestHashMarket.sol. (The Solidity test codes that are the same as the original text are omitted here to avoid duplication)

JavaScript Test

Truffle allows us to use JavaScript for testing, leveraging the Mocha test framework. (The same JavaScript test code as the original text is omitted here to avoid duplication)

FAQs (FAQs) about using Truffle to test smart contracts: (The same FAQ section as the original text is omitted here to avoid duplication)

The above is the detailed content of Truffle: Testing Smart Contracts. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn