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:
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:
mkdir HashMarket cd HashMarket truffle init
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.
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):
npm install -g ganache-cli
, open a separate terminal window or tab and run:
ganache-cli
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:
module.exports = { networks: { development: { host: "127.0.0.1", port: 8545, network_id: "*" } } };
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)
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)
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.
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)
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!