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 helloWorld.js in the test/ directory.
  2. Add the following content to the helloWorld.js file:

Javascript




const HelloWorld = artifacts.require("HelloWorld") ;
  
contract("HelloWorld" , () => {
    it("Hello World Testing" , async () => {
       const helloWorld = await HelloWorld.deployed() ;
       await helloWorld.setName("User Name") ;
       const result = await helloWorld.yourName() ;
       assert(result === "User Name") ;
    });
});


  • HelloWorld: The smart contract we want to test, We begin our test by importing our HelloWorld contract using artifacts.require.
  • To test the setName function, recall that it accepts a name(string) as an argument.
  • Also, the yourName variable in our contract is using public modifier, which we can use as a getter from the outside function.
  • 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(result === “User Name”) ;.

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 – Hello World Dapp

Flutter and Blockchain

This tutorial will take you through the process of building your first mobile dapp – Hello World Dapp!

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 your first 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

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 .

Once we have node installed, we only need one command to install Truffle:

npm install -g truffle

We will also be using Ganache, a personal blockchain for Ethereum development you can use to deploy smart contracts, develop applications, and run tests. You can download Ganache by navigating to http://truffleframework.com/ganache and clicking the “Download” button.

Creating a Truffle Project

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

Directory Structure

  • contracts/ : Contains solidity contract file.
  • migrations/: Contains migration script files (Truffle uses a migration system to handle contract deployment).
  • test/ : Contains test script files.
  • truffle-config.js: Contains truffle deployment configurations information.

Similar Reads

Writing your first 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...