By the end of this lesson, you will be able to:
In the previous lessons, we outlined how to write, compile and deploy smart contract written in Solidity programming language for use on the BNB Smart Chain network using the different IDEs. In this lesson, we perform unit testing of our smart contract to make sure that its works perfectly and as desired before deploying it onto blockchain network.
In this hands-on guide, readers are encourged to perform these tasks along for a better understanding. Before starting off with the practical demo, it is necessary to make sure you have the following software requisites installed. For this tutorial, we will be using Truffle IDE for unit testing our smart contracts.
We will test out our Solidity smart contracts behaviour with unit testing with Truffle which uses Chai and Mocha frameworks. In web development lifecycle, unit testing is needed to do to ensure the code is working the way as it is expected.
In order to be able to complete this demo, make sure you have followed the tutorial Hello World Smart Contract for writing the HelloWorld
smart contract using the Truffle IDE and the lesson on Compiling Smart Contracts for successfully compiled the HelloWorld
smart contract.
The firt step is to create a file in the test
folder that will contain the test script. In the test
folder create a new file hello_world.js
In the hello_world.js
file residing in the test
folder, copy and paste the code given below.
var helloworld = artifacts.require('../contracts/bsc/HelloWorld');
contract('HelloWorld', function(accounts) {
let instance;
before(async () => {
instance = await helloworld.deployed();
});
//Test to check if the default value is set to "hello, world"
it('Default message should be hello, world',async () => {
let message = await instance.getMessage.call({from: accounts[0]});
assert.equal(message, "Hello, World","Incorrect Default Value");
});
//Test to check if the setName is working or not
it('Should save name',async () => {
let result = await instance.setName.sendTransaction('BNB Chain',{from: accounts[0]});
let message = await instance.getMessage.call({from: accounts[0]});
assert.equal(message, "Hello, BNB Chain","Value Could not be Set");
});
//Test to check if error is thrown on empty name field
it('Should throw error on empty name',async () => {
try{
let result = await instance.setName.sendTransaction('',{from: accounts[0]});
assert.fail(true,false,"The function should throw error");
}
catch(err){
assert.include(String(err),'revert','throws different error');
}
});
});
artifacts.require()
method. In our case, the smart contract is stored in the ../contracts/bsc/HelloWorld
.greet
variable is World
. If not, an message Incorrect Default Value
is displayed.Value Could not be Set
is displayed.greet
variable is not set to empty value.ganache-cli
using the command ganache-cli
.ganache-cli
is running in the background. This is important because testing is done on the local network.truffle test --config=truffle-config-bsc.js
to run the tests.In this tutorial, we covered unit testing of smart contracts using the Truffle IDE that uses Chai and Mocha frameworks for the purpose of unit testing. In order to be able to successfully complete this demo, make sure you have a truffle project created with HelloWorld smart contract as specified in the Using Truffle lesson.