Testing is an integral part of a software development process that allows us to inspect the outcomes of the program and gives us confidence at the time of software distribution to end-users.
In the previous article, we illustrated building a lottery contract on Morpheus Labs BPaaS. Today, we will talk about testing your work. In this way, we ensure the contract works as we designed it to.
Setting up the application directory.
First of all, we must set up Truffle. Truffle is a suite of tools that makes the testing, deployment, and overall development of Smart Contracts way easier.
There is no need to install the truffle suite as it is already in the stack selected in our workspace. However, there is some set up to do.
First, let’s open a new terminal tab in our CDE.
Clicking the + icon at the bottom, and select terminal.
We are going to make use of the command truffle init.
The truffle init command must be run in an empty directory, but our app directory already has the file lottery.sol.
We will temporarily create an empty directory which we would call temporaryDir to run the command.
After the initiation process runs, we will have a set of directories and configuration files that will aid us in our development process. However, we should place them in our app directory by running the commands.
Our sample file structure should look like this:
As we can see, there is another file called Migrations.sol located in the contracts directory. This file is created by truffle, and it is actually a smart contract designed for the deployment of our contracts.
Finally, we must compile our contract by running the command in the environment set up before creating the tests.
This command will create a new directory called builds, which will include .JSON files that describe the contracts written in solidity as an ABI and Bytecode.
Creating the tests
Truffle comes with a framework to automate tests, based on Mocha.
First create a new file under the test directory by right-clicking it, selecting new, and then file.
In lottery.js, we create 5 tests to verify the following:
- Assert that the manager_address and deployer_address are the same
- A new player can enter the lottery
- A new player to the lottery sends more than 0.1 ether to join
- a player cannot call the selectWinner function
- The manager can call the selectWinner function
In your DApp, you can specify your own tests based on the intentions of your smart contract and verify it in a similar format.
Running the tests
Be sure that we are located in our lotteryapp directory in the terminal. Now, let’s run the next command to start our tests.
If all the test pass successfully, we should see an output similar to:
Now that these tests have passed, we can be more confident in the deployment of our contract, which we will talk about in the next article.
About Morpheus Labs
Morpheus Labs, a Singapore homegrown start-up that aims to develop a comprehensive full-stack blockchain platform and marketplace, offering an infrastructure for enterprises and developers to build and experiment with their own blockchain applications effortlessly at minimal cost and time. This provides a one-stop development tool, for smart contracts, back-end services, and front-end applications (DApp). It provides easy access and mission-critical tools that enable developers to develop, test and manage blockchain applications using different Blockchain Protocols to achieve cost efficiency and a fail-safe environment.
From Proof of Concept (POC), Pilot projects for blockchain solutions, to large scale enterprise applications to improve and automate business processes, Morpheus Labs empower businesses with digital transformation and innovation by providing consultation and Implementation Services in Digitalization, Process Automation and Technology Innovation mainly in areas of Blockchain, RPA and IoT related technologies.