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.
- Create a new file named population.js in the test/ directory.
- 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:
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:
- Setting up the development environment
- Creating a Truffle Project
- Writing the Smart Contract
- Compiling and Migrating the Smart Contract
- Testing the Smart Contract
- Contract linking with Flutter
- Creating a UI to interact with the smart contract
- 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: