Testing a Smart Contract on Morpheus Labs BPaaS

Morpheus Labs Team
May 26 · 5 min read
Image for post
Image for post

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.

Image for post
Image for post

Clicking the + icon at the bottom, and select terminal.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

Our sample file structure should look like this:

Image for post
Image for post

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.

Image for post
Image for post

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.

We will write a javascript test file that accesses our lottery contract and interacts with it over an Ethereum local test network, that runs every time we run our tests.

First create a new file under the test directory by right-clicking it, selecting new, and then file.

Image for post
Image for post
Image for post
Image for post

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
Image for post
Image for post

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.

Image for post
Image for post

If all the test pass successfully, we should see an output similar to:

Image for post

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.


Be a part of our community! Join our official group chat on Telegram:

>> https://t.me/morpheuslabs

(Check out our Community Telegram Guidelines.)


🤳🏼Connect with us:

LinkedIn | Twitter | Telegram | Facebook | Youtube | Github | Reddit | CrunchBase | Website | Gitter

Morpheus Labs

Powering Blockchain Worldwide

Morpheus Labs Team

Written by

The Blockchain-Platform-as-a-Service (BPaaS) for prototyping, deploying and operating Dapps at minimal cost and time. | https://morpheuslabs.io

Morpheus Labs

The Blockchain-Platform-as-a-Service (BPaaS) for prototyping, deploying and operating Dapps at minimal cost and time. | https://morpheuslabs.io

Morpheus Labs Team

Written by

The Blockchain-Platform-as-a-Service (BPaaS) for prototyping, deploying and operating Dapps at minimal cost and time. | https://morpheuslabs.io

Morpheus Labs

The Blockchain-Platform-as-a-Service (BPaaS) for prototyping, deploying and operating Dapps at minimal cost and time. | https://morpheuslabs.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store