Testing the Smart Contract

In Truffle, we can write tests either in JavaScript or Solidity, In this article, we’ll be writing our tests in Javascript using the Chai and Mocha libraries.

  1. Create a new file named population.js in the test/ directory.
  2. Add the following content to the population.js file:

Javascript




const Population = artifacts.require("Population") ;
  
contract("Population" , () =>{
    let population = null ;
    before(async () => {
        population = await Population.deployed() ;
    });
  
    it("Setting Current Population" , async () => {
        await population.set("India" , 1388901219) ;
        const name = await population.countryName() ;
        const pop = await population.currentPopulation();
        assert(name === "India") ;
        assert(pop.toNumber() === 1388901219) ;
    });
  
    it("Decrement Current Population" , async () => {
        await population.decrement(100) ;
        const pop = await population.currentPopulation() ;
        assert(pop.toNumber() === 1388901119);
    });
  
    it("Increment Current Population" , async () => {
            await population.increment(200) ;
            const pop = await population.currentPopulation() ;
            assert(pop.toNumber() === 1388901319);
        });
});


  • Population: The smart contract we want to test, We begin our test by importing our Population contract using artifacts.require.
  • Testing the set,decrement and increment functions of Population.sol smart contract by providing it with some defined values.
  • Truffle imports Chai so we can use the assert function. We pass the actual value and the expected value, To check that the name is set properly or not, assert(name === “India”) ; .
  • For the currentPopulation, it returns BigNum object but Javascript doesn’t deal with it, Therefore converting the BigNum object to the regular Javascript object for check as assert(pop.toNumber() === 1388901219) ;

Running the tests

  • Running the test as:
truffle test
  • If all the test pass, you’ll see the console output similar to this:

truffle test

Flutter and Blockchain – Population Dapp

Before checking out this article, Do take a look at Flutter and Blockchain – Hello World Dapp. This tutorial will take you through the process of building your mobile dapp – Population on Blockchain!

This tutorial is meant for those with a basic knowledge of Ethereum and smart contracts, who have some knowledge of the Flutter framework but are new to mobile dapps.

In this tutorial we will be covering:

  1. Setting up the development environment
  2. Creating a Truffle Project
  3. Writing the Smart Contract
  4. Compiling and Migrating the Smart Contract
  5. Testing the Smart Contract
  6. Contract linking with Flutter
  7. Creating a UI to interact with the smart contract
  8. Interacting with the complete Dapp

Description

Population on blockchain is a simple decentralized application, which will allow you to store a specific country population on the blockchain, you can increment as well as decrement the population based on the current condition of the country.

Output:

Similar Reads

Setting up the development environment

Truffle is the most popular development framework for Ethereum with a mission to make your life a whole lot easier. But before we install truffle make sure to install node ....

Creating a Truffle Project

Create a basic Flutter project in your favorite IDE Initialize Truffle in the flutter project directory by running...

Writing the Smart Contract

The Smart Contract actually acts as the back-end logic and storage for our Dapp....

Compiling and Migrating

...

Testing the Smart Contract

...

Contract linking with Flutter

...

Creating a UI to interact with the smart contract

...

Interacting with the complete Dapp

Compilation...