Tuesday, 12 June, 2018 UTC


In our introduction to Truffle, we discussed what Truffle is and how it can help you automate the job of compiling, testing and deploying smart contracts.
In this article, we’ll explore how to test smart contracts. Testing is the most important aspect of quality smart contract development.
Why test extensively? So you can avoid stuff like this, or this. Smart contracts handle value, sometimes a huge amount of value — which makes them very interesting prey for people with the time and the skills to attack them.
You wouldn’t want your project to end up on the Blockchain graveyard, would you?
Getting Started
We’re going to be making HashMarket, a simple, smart-contract based used goods market.
Open your terminal and position yourself in the folder where you want to build the project. In that folder, run the following:
mkdir HashMarket
cd HashMarket
truffle init
You should get a result that looks a little bit like this:
Setting up...
Unbox successful. Sweet!


  Compile:        truffle compile
  Migrate:        truffle migrate
  Test contracts: truffle test
You’ll also get a file structure that looks like this:
├── contracts
│   └── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
├── test
├── truffle-config.js
└── truffle.js
For a refresher on the files, take a look at the previous article. In a nutshell, we have the basic truffle.js and the two files used for making the initial migrations onto the blockchain.

Preparing the test environment

The easiest way to test is on the local network. I highly recommend using the ganache-cli (previously known as TestRPC) tool for contract testing.
Install ganache-cli (which requires the Node Package Manager):
npm install -g ganache-cli
After that, open a separate terminal window or tab and run this:
You should see an output similar to this:
Ganache CLI v6.1.0 (ganache-core: 2.1.0)

Available Accounts
(0) 0xd14c83349da45a12b217988135fdbcbb026ac160
(1) 0xc1df9b406d5d26f86364ef7d449cc5a6a5f2e8b8
(2) 0x945c42c7445af7b3337834bdb1abfa31e291bc40
(3) 0x56156ea86cd46ec57df55d6e386d46d1bbc47e3e
(4) 0x0a5ded586d122958153a3b3b1d906ee9ff8b2783
(5) 0x39f43d6daf389643efdd2d4ff115e5255225022f
(6) 0xd793b706471e257cc62fe9c862e7a127839bbd2f
(7) 0xaa87d81fb5a087364fe3ebd33712a5522f6e5ac6
(8) 0x177d57b2ab5d3329fad4f538221c16cb3b8bf7a7
(9) 0x6a146794eaea4299551657c0045bbbe7f0a6db0c

Private Keys
(0) 66a6a84ee080961beebd38816b723c0f790eff78f0a1f81b73f3a4c54c98467b
(1) fa134d4d14fdbac69bbf76d2cb27c0df1236d0677ec416dfbad1cc3cc058145e
(2) 047fef2c5c95d5cf29c4883b924c24419b12df01f3c6a0097f1180fa020e6bd2
(3) 6ca68e37ada9b1b88811015bcc884a992be8f6bc481f0f9c6c583ef0d4d8f1c9
(4) 84bb2d44d64478d1a8b9d339ad1e1b29b8dde757e01f8ee21b1dcbce50e2b746
(5) 517e8be95253157707f34d08c066766c5602e519e93bace177b6377c68cba34e
(6) d2f393f1fc833743eb93f108fcb6feecc384f16691250974f8d9186c68a994ef
(7) 8b8be7bec3aca543fb45edc42e7b5915aaddb4138310b0d19c56d836630e5321
(8) e73a1d7d659b185e56e5346b432f58c30d21ab68fe550e7544bfb88765235ae3
(9) 8bb5fb642c58b7301744ef908fae85e2d048eea0c7e0e5378594fc7d0030f100

HD Wallet
Mnemonic:      ecology sweet animal swear exclude quote leopard erupt guard core nice series
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545
This is a listing of all the accounts ganache-cli created for you. You can use any account you wish, but these accounts will be preloaded with ether, so that makes them very useful (since testing requires ether for gas costs).
After this, go to your truffle.js or truffle-config.js file and add a development network to your config:
module.exports = {
    networks: {
      development: {
        host: "",
        port: 8545,
        network_id: "*"
The post Truffle: Testing Smart Contracts appeared first on SitePoint.