Testing the Contract

Testing is an integral part of the software development process because testing allows us to automate processes that inspect the outcomes of the program, and gives us confidence at the time of distributing the software to final users.

Now that we have built the lottery contract, it is time to test it. In that 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 use 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.

Hence, we will temporarily create an empty directory which we would call temporaryDir to run the command.

$  mkdir temporaryDir

Then we enter the directory and run the command:

$ cd temporaryDir
$ truffle init

After the initiation process runs, we will have a set of directories and configuration files that will aid us in our development process, but we should place them in our app directory by running the commands.

$ mv ./* ../lotteryapp

We can now delete the temporary directory.

$ cd ..
$ rm -r temporaryDir

Then, we enter our app directory and locate our lottery.sol file in the directory contracts, which was created with truffle init.

$ cd lotteryapp
    $ mv lottery.sol contracts/.

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 compile our contract by running the command to set up our environment before creating the tests.

$ truffle compile

This command will create a new directory called builds, which will include .JSON files describing 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.

We will name it lottery.jsWe will name it lottery.js

We will name it lottery.js

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 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.

$ truffle test

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

Now that these tests have passed, we can be more confident in the deployment of our contract.