Solidity CRUD Tutorial, Part 2 — Testing Your Smart Contract with Truffle

Testing a Solidity smart contract using Truffle framework

Gaurav Agrawal
Crowdbotics
5 min readOct 9, 2018

--

In the last tutorial, we have created a smart contract which performs some basic CRUD (create, read, update, delete) operation, let’s test that smart contract using Truffle.

Truffle — Truffle is the most popular development and testing framework for developing ethereum smart contracts.

Installing Truffle

Prerequisites

Installation

  1. First, we need to install the Truffle package
  1. Verify that it has been downloaded successfully by running

Choose a directory and run

It will create a directory structure and create few files.

Let’s go to these directories one by one an will understand what they are.

  • contracts/: Directory for Solidity contracts
  • migrations/: Directory for scriptable deployment files
  • test/: Directory for test files for testing your application and contracts
  • truffle.js: Truffle configuration file

We will use truffle develop to test our smart contract. Truffle gives us inbuilt development blockchain with ethereum accounts.

Let’s create a file with CrudApp.sol (Notice, same name as our contract name) under contracts directory and paste our smart contract code.

Let’s compile our smart contract using,

It may show some warnings but let’s ignore them for now. Open our development console using,

It will show an output something like below:

Your accounts and private keys will be different than mine.

Now we have accounts ready and our development blockchain spun up, we can test our smart contract.

Before that, let’s run few commands and test the balance of our accounts. Replace my account number with your account in the command below.

As you can see, above command gives result in BigNumber. You need to convert that into number format using .toNumber() function.

This shows balance in WEI (smallest ethereum unit / 1 ethereum = 1⁰¹⁸ wei). To convert this into the ether, you need to use web3.fromWei() function.

Image source

Now, we need to deploy our smart contract.

You can use either deploy command or migrate command. You don’t need to prefix truffle with commands on truffle CLI.

You can use reset flag ( — reset) while redeploying your contracts to get the initial state of the blockchain.

Test Cases-

Now let’s write some test cases to test our smart contract.

Create a file CrudApp.js (name should be same as smart contract file) under test directory and copy below code.

Let’s walk through our code and understand it.

We imported our contract and assert library, which created an instance of our contract after deploying it (CrudApp.deployed()). Then we simply wrote three test case in which we are testing all four operations.

First, we are inserting a record and then updating and deleting it, respectively.

You can write test cases in Solidity too, but we choose Javascript, as Truffle makes it easy to test our contract.

You can also run few commands directly on Truffle console and also interact with our smart contract using Truffle CLI (Command Line Interface).

Let’s get an instance of our smart contract using this command.

This will give us an instance (crud) of our smart contract, let’s play with it. This instance is simple ABI (Application Binary Unterface) you can check it just printing crud.

ABI is the interface between two program modules, one of which is often at the level of machine code. The interface is the de facto method for encoding/decoding data into/out of the machine code.

Let’s check the total number of countries by running this command.

Let insert a new country

The result should be something like this.

This is transaction receipt you can see that our country event gets fired too. Whenever you change blockchain state, it has to be a transaction and transactions consume gas.

Now let’s check our entry by running this command

You should see a result something like this.

You’ve now learned how to interact with smart contract, and how to test it!

Show me what you’ve built in the comment section.

Conclusion

We have learned how to do basic CRUD operations using solidity and test our smart contract. We cut down on details which can be overwhelming for the sake of brevity. Solidity and Truffle have extensive documentation and you should read them. Also, we used web3.js which comes with Truffle bundle.

There are multiple ways to optimize our CRUD operations (Hint : you can use an external array). Optimize it and let us know.

It’s time for some 👏.

Starting a new blockchain project, or looking for a Solidity developer?

Crowdbotics helps business build cool things with Solidity (among other things). If you have a blockchain project where you need additional developer resources, drop us a line. Crowbotics can help you estimate build time for given product and feature specs, and provide specialized Solidity developers as you need them. If you’re building with Solidity, check out Crowdbotics.

--

--