In this lesson, we will be covering the theory behind testing Ethereum smart contracts, with a focus on TruffleMocha, and Chai. You will need an intermediate level of knowledge of Solidity and JavaScript to help you get the most out of these lessons.

If you don't have prior exposure to Solidity, or you want to revise some of the concepts, go ahead and start learning with our first lesson.

If you are not comfortable with JavaScript, consider going through a tutorial elsewhere before starting this lesson.

Let's Peep Into Our Project

If you followed along with our previous lessons, you should have built a zombie-themed game that is largely ready, and your file structure should look like this:

├── build
  ├── contracts
      ├── Migrations.json
      ├── CryptoZombies.json
      ├── erc721.json
      ├── ownable.json
      ├── safemath.json
      ├── zombieattack.json
      ├── zombiefactory.json
      ├── zombiefeeding.json
      ├── zombiehelper.json
      ├── zombieownership.json
├── contracts
  ├── Migrations.sol
  ├── CryptoZombies.sol
  ├── erc721.sol
  ├── ownable.sol
  ├── safemath.sol
  ├── zombieattack.sol
  ├── zombiefactory.sol
  ├── zombiefeeding.sol
  ├── zombiehelper.sol
  ├── zombieownership.sol
├── migrations
└── test
. package-lock.json
. truffle-config.js
. truffle.js

See the test folder? This is where we are going to be putting our tests.

Truffle provides support for tests written in JavaScript and Solidity but, for the scope of this lesson, we are going to keep things simple and stick to JavaScript.

Build Artifacts

Every time you compile a smart contract, the Solidity compiler generates a JSON file (referred to as build artifacts) which contains the binary representation of that contract and saves it in the build/contracts folder.

Next, when you run a migration, Truffle updates this file with the information related to that network.

The first thing you'll need to do every time you start writing a new test suite is to load the build artifacts of the contract you want to interact with. This way, Truffle will know how to format our function calls in a way the contract will understand.

Let's look at a simple example.

Say there was a contract called MyAwesomeContract. We could do something like the following to load the build artifacts:

const MyAwesomeContract = artifacts.require(“MyAwesomeContract”);

The function returns something called a contract abstraction. In a nutshell, a contract abstraction hides the complexity of interacting with Ethereum and provides a convenient JavaScript interface to our Solidity smart contract. We'll be using it in the next chapters.

The contract() function

Behind the scenes, Truffle adds a thin wrapper around Mocha in order to make testing simpler. Since our course focuses on Ethereum development, we won't be spending much time explaining the bits and bytes of Mocha. If you're inclined to learn more about Mocha, check out their website, once you're done with this lesson. For now, you only have to understand what we cover here - how to: