Testing your Solidity smart contracts

A good practice in the software industry is writing tests for code. The tests needs to cover the features of your code and assert that everything is working as predicted. As we are writing smart contracts that can deal with money, writing tests is a really important task for us. The Embark Framework offers a pretty rich environment for testing our smart contracts.

We will write our tests in Javascript using the Mocha. To install it just type npm install -g mocha  in your terminal.

Let’s start writing test for our MDBService  contract that we deployed in our previous tutorial. We want to test the functionalities of our contract:

  • It’s empty when initialized
  • When a user registers, it creates a contract
  • We can retrieve a contract made by a user
  • A user can’t register more than one time

We will create a new file in the test directory  test/MDBService_spec.js .  We will import the libraries and tools we need to test our code:

Following the Mocha syntax we will describe our tests and deploy all of the contracts in our project:

Our first test will check if when the MDBService , it is really empty:

Then we will check if an account is really created after we call the register function:

Then we test if the contract has a correct address and our method to access the contracts works:

And finally we’ll unsure one user can’t create multiple accounts:

Even if smart contracts code are by nature pretty short, writing tests is really important to help you identify errors while building an application. It is also important as in smart contracts development, we often re use existing contracts code and it is important to know that your bricks are sane and safe.

To run your tests using Embark Framework, in a terminal type:

With our current test we should see this:

Here is our complete test file:


Deploying our first contracts with Embark Framework

Since we installed Embark Framework and made a quick tour of it, we’ll now use it to create our first DApp: Decentralized Application. Our DApp will have two parts:

  • Smart contract – It will live on the blockchain  and handle the logic of our application. Coded with Solidity.
  • User interface – Will let normal users interact with the contract and is built with standard web technologies: HTML, CSS, Javascript.

Our DApp will be a decentralized micro blogging platform  (read Twitter clone). The functionalities will be:

  • A user can register
  • A user can post a message
  • List all the accounts
  • List all the messages of a user

Our application will be called MDB which stands for Micro Decentralized Blog. For this we will write two smart contracts, the first one is the actual application that will store all the accounts and let the user register: MDBService. The second part will be the user account containing all the tweets of the user: MDBAccount. When a user register using the application contract, it will actually create an account and store it’s address similar to what we did in our article about factories.

In this tutorial we will only write the smart contract part, we’ll cover the user interface and other topics such as writing tests in next tutorials.

First, we will use Embark Framework to create our code base. In your favorite terminal type:

Embark created the folders for our application, you can load the folder in your favorite IDE and we’ll create our first Smart contract: the user account. We will call it MDBAccount.sol and place it in the folder app/contracts .


We will use the struct  keyword to define a new type, a Post. A Post will store the message that was posted and the timestamp the user posted the message.

If you already followed the basic tutorials, the code is pretty straightforward:

We will try and deploy this first contract. In a terminal we will start our testing blockchain:

embark simulator

and then in an other terminal window:

embark run

We can see our contract was deployed.

So let’s try to see if our contract works, we’ll access the contract from the console using:  MDBAccount.getNumberPosts().toNumber()  and  MDBAccount.getOwnerAddress .

As you can see, we have a serious problem, the owner address is set as 0x00 which is definitely not ours. So due to the modifier we won’t be able to interact with our contract. We need to tell Embark to supply our address as the first argument of the constructor.

For this, we’ll go in the config/contracts.json  file and past the address of our contract. To know it, just type: web3.eth.accounts[0]  in the Embark console. Mine is: 0x4d89d68b3325e620339fbd3c5ad7c4a8c22cd7dd . So let’s add it on the contracts configuration file:

If you save the file, you’ll see that the Embark console will deploy the contract again automatically, we can now play with our contract:


You can try yourself using the following calls:

  • MDBAccount.getNumberPosts().toNumber()
  • MDBAccount.post("Hello World")
  • MDBAccount.getPost(0)[0]  //Getting the message of the post
  • MDBAccount.getPost(0)[1].toNumber()  //Getting the timestamp

It was nice to play around with our simple contract, we will now make the MDBService  in a new app/contracts/MDBService.sol  file which will be in charge of managing the accounts.  As our MDBService contract will use the MDBAccount contract we need to import it. The import keyword just copy the content of a file.

The whole code for registering our new accounts and retrieving them:

As the accounts contract will be created by our MDBService contract, we need to specify to Embark that we don’t want it to be deployed in the file config/contracts.json .

Now your contract should appear as not deployed in the list:

We can quickly test our contract by calling register and get the address of our account with the console.

This is everything for the smart contract part of our brand new DApp. We voluntary didn’t test much interacting with the service and sending data to our post through it because in the next tutorial we will cover good practice for testing your smart contracts with Embark Framework.


Getting started with Embark Framework

In the previous articles we saw all the basics of Ethereum smart contracts development with Solidity. We used Ethereum Wallet and it was great! We were able to have a simple and easy to setup small dev environment. As you figured out it was nice for getting started but what if we want to go deeper? We want to write bigger applications, be able to use multiple source files. Write tests, debug, use a versioning system, deploy multiple contracts at once ect…

To go deeper we’ll use the Embark Framework. Embark Framework is easy to get started with and has a lot of features:

Blockchain (Ethereum)

  • Automatically deploy contracts and make them available in your JS code. Embark watches for changes, and if you update a contract, Embark will automatically redeploy the contracts (if needed) and the dapp.
  • Contracts are available in JS with Promises.
  • Do Test Driven Development with Contracts using Javascript.
  • Keep track of deployed contracts; deploy only when truly needed.
  • Manage different chains (e.g testnet, private net, livenet)
  • Easily manage complex systems of interdependent contracts.

Decentralized Storage (IPFS)

  • Easily Store & Retrieve Data on the DApp through EmbarkJS. Including uploading and retrieving files.
  • Deploy the full application to IPFS or Swarm.

Decentralized Communication (Whisper, Orbit)

  • Easily send/receive messages through channels in P2P through Whisper or Orbit.

Web Technologies

  • Integrate with any web technology including React, Foundation, etc..
  • Use any build pipeline or tool you wish, including grunt, gulp and webpack.


Installing Embark framework

You can get started on any platform, here are the tools you need:

Once you got this installed, we’ll use NPM to install the embark framework.

Embark framework Hello World

The Embark framework contains a basic demo we can deploy to understand how it works.  To deploy it type in your terminal the following command, it will create a new folder called embark_demo  containing the source code of the demo.

To lunch the demo we’ll need to lunch the simulator. The simulator has two advantages, it will create a blockchain on your computer so your computer will be able to mine when needed to deploy contracts and do other transactions and you don’t have to download all the Ethereum blockchain which will save you space!

Let it run on a terminal window and in a new terminal window you’ll start the Embark demo application with:

On your terminal, the embark dashboard will take life, it should look like this:

Here are the elements:

  1. The list of your deployed contracts and there addresses, in the Embark demonstration sample, there is only one contract: Simple Storage  that have a simple mission: storing one value and make this value readable from outside.
  2. The services that are currently running. We’ll explain the server running on port 8000 in a minute.
  3. The logs of what happened
  4. A console that let’s you interact with your contracts.

So let’s first check the code of the contract that lives in the app/contracts  folder of the demo:

In the app folder, you will also notice the css , images  and js  folders. They contains the sources for building web applications that will interact with the blockchain. Let’s now check what the Embark demo web app does. You’ll need to visit  http://localhost:8000/ with your favorite browser.

Note that if you have some apps already using the port 8000, you can change the port used by Embark in the folder config/webserver.json .

So you can see we have a simple interface to interact with our contract, we can set the value stored in the contract, get it and see  the calls that were made to interact with it.

The contract calls that were made can also be used in the console we saw in the Embark dashboard.

This was our walk around the Embark framework, in the next lessons we’ll create our own application using the Embark framework.

In the next tutorial we will start developing our first application with Embark Framework and deploy our first contracts.