Вы находитесь на странице: 1из 21

9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

A BEGINNER FRIENDLY TUTORIAL

Test Driven Solidity with Tru e


Unit-Testing of Smart Contracts with Tru e, Ganache, Mocha, and Chai
Muhammad Yahya Follow
Nov 22, 2018 · 11 min read

Introduction
It is very common to write and compile Solidity code manually which is fine for small
projects. However, as our project is growing bigger and bigger, it is good to have an
automatic way of smart contracts development. In addition, testing Solidity code is
crucial to avoid any problematic situation caused by a bug in the smart contract. There
are different frameworks available for taking care of the development process. Truffle is
one of them and is often regarded as the Ethereum Swiss Knife framework as it is a
development environment, testing framework and asset pipeline for Ethereum.

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 1/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

Objectives
This tutorial is aimed to get started with Truffle and write some tests to check the basic
features offered by Solidity.

1. Create a truffle project and configure a development network

2. Create and deploy smart contracts

3. Interact with the smart contract from Truffle console

4. Write tests for testing main features offered by Solidity

Prerequisites
1. Download and install node.js

2. Install Truffle globally:


In order to install truffle globally on your machine, open a terminal and run this
command
$ npm install truffle -g

3. Install ganache-cli:
Ganache is a personal and local blockchain for Ethereum development and provides
a very good environment to deploy smart contracts and run tests. Ganache is
available as a desktop application as well as a command line tool. In this tutorial, we
are going to install ganache as a command line tool by running the following
command in the terminal:
$ npm install ganache-cli -g

1. Creating a Truffle Project


In the next few lines of code, we are going to create a directory and initialize it with
Truffle to create a basic project skeleton.

$ mkdir truffle-demo

$ cd truffle-demo

$ truffle init
https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 2/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

On successful initialization, we will see the following message in the console:

1 Downloading...
2 Unpacking...
3 Setting up...
4 Unbox successful. Sweet!
5 Commands:
6 Compile: truffle compile
7 Migrate: truffle migrate
8 Test contracts: truffle test

truffle-project.sh hosted with ❤ by GitHub view raw

If we look at the newly created directory, Truffle has created a boilerplate file structure:

1 C:.
2 | truffle-config.js
3 | truffle.js
4 |
5 +---contracts
6 | Migrations.sol
7 |
8 +---migrations
9 | 1_initial_migration.js
10 |
11 \---test

truffle-folder.sh hosted with ❤ by GitHub view raw

Let’s have a brief walk through of these files and folders:

truffle.js
This is the configuration file that contains information such as development network
settings for the project. This file will be used to configure the development network.

truffle-config.js
This is the same file as truffle.js and used in case of windows system run into a
conflict problem while executing the generic Truffle commands. So, in that case, this file

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 3/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

will be used instead of truffle.js file.

contracts
This folder is a container for all of our smart contracts. Truffle goes to the contracts
folder and looks for the compatible files ( .sol ) to compile while compiling the project.
The folder already contains a Migrations.sol file.

migrations
The configurations for migrating the contract over the network are written in a
JavaScript file. These settings basically define how (for example: in what order) smart
contracts will be deployed over the blockchain. All those migration files will go inside
the migration folder. This folder contains a file 1_initial_migration.js that will deploy
the Migrations.sol contract to the blockchain.

test
All the tests — either written in Solidity or JavaScript — will go to this folder.

In addition, Truffle will create a build folder to hold artifacts of the compiled contracts.
We will see this folder when we run $ truffle compile command but before that, we
need to set up our private network first.

2. Preparing the Test Environment


After having installed ganache-cli, open a terminal and run

$ ganache-cli

Ganache has created ten accounts with some amount of ether for executing the
development tasks. It also opens the RPC port 8545 at localhost .

1 Ganache CLI v6.1.8 (ganache-core: 2.2.1)


2
3 Available Accounts
4 ==================
5 (0) 0x5a610c431673ac449a3b340fa55d5ce03f6719f8 (~100 ETH)
6 (1) 0xf96c3666fc20f74389811287d3dbbb2d7830d8c4 (~100 ETH)
( ) bd d bf b f ( )
https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 4/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium
7 (2) 0xc0161bd0d39765bfcc3c3317b576ee7813fe5a0c (~100 ETH)
8 (3) 0x35ef9eba8886cb69f409b79299325cf36ec89e87 (~100 ETH)
9 (4) 0xc25e0bb5e37bf9bbb72a81527ee504be62c7f380 (~100 ETH)
10 (5) 0x813a1b2267f722c9f0d5de6290ba985fbd13e96e (~100 ETH)
11 (6) 0x040f9c11da796998790578efe93ae8874443bbe5 (~100 ETH)
12 (7) 0x885250407d0e94478e4b2b2b78229eda45b011bb (~100 ETH)
13 (8) 0x7da839bfeca4f8455099b21f1372be238c8e9990 (~100 ETH)
14 (9) 0x53a9152920c154327fb4ef96620188eaeedd4954 (~100 ETH)
15
16 Private Keys
17 ==================
18 (0) 0xced73bf53f8cdafa509e8184d161b5075a944c53f0afe533c751775dc649cd85
19 (1) 0x7a187d8638d48ea88d0321d042bd2caf895d25ec176f254835f7364468107173
20 (2) 0xde0cb86b2bf735b9a9156139e896a3cd3c990d96676c1f7ee4d264c5d08d29eb
21 (3) 0xbb2aadb39f34293cd91da7bfb201976c4db297541c16e26e167bf8dd13ec0be9
22 (4) 0x2c0eeb27cdef734b431f2d255f46822d16149c4b35efafc6f7cf645fdf3709f6
23 (5) 0xdc3a8aa55155430b9f7a6b09817ec2e2d4cb3ac250ba99344bf08b8be72821d0
24 (6) 0x3fa6fd3046abfeb16b1cf4050b69e934489191b052dae4cc9b7afb81617ce07b
25 (7) 0x76be3c6b053f8c3222f9d96729342dd43a6d6402f41afd3dd420d15727b63c40
26 (8) 0x65b2d1b62c49f768074ede589f7a1a99a2b4b6aa093109e11e0cccddd501851e
27 (9) 0x49c52c620ba2343686f4c338180a3888f6249fa75f425a994cee42b4397fe29c
28
29 HD Wallet
30 ==================
31 Mnemonic: tenant black hollow shine arrest fatal reunion blush chest toe dragon crowd
32 Base HD Path: m/44'/60'/0'/0/{account_index}
33
34 Gas Price
35 ==================
36 20000000000
37
38 Gas Limit
39 ==================
40 6721975
41
42 Listening on 127.0.0.1:8545

ganache.sh hosted with ❤ by GitHub view raw

Next, we need to configure network settings in truffle.js file as shown below:

1 module.exports = {
2 networks:{
3 development:{
https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 5/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

4 host: '127.0.0.1',
5 port: 8545,
6 network_id: '*' // match any netwrok id
7 }
8 }
9 };

truffle.js hosted with ❤ by GitHub view raw

In these lines of code, we have configured a single development network running on


127.0.0.1:8545 .

Line 2 defines an object which is keyed by a network name that is development in


this case.

On the next lines, host and port parameters of the network are configured.

Line 6 gives a development network that matches any network it connects to.

So far we have initiated our truffle project, set up private network and ganache-cli
running. In order to check if everything is set up correctly, type:

$ truffle test

We will see the following output:

Compiling .\contracts\Migrations.sol...

0 passing (1ms)

Now we are good to go!

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 6/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

3. Smart Contract Development with Truffle


The general workflow for smart contract development with Truffle can be accomplished
with the following steps:

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 7/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

Writing the Smart Contract


Let’s create a simple smart contract by typing the following command in the terminal:

$ truffle create contract SimpleContract

In the contracts folder, Truffle has just created SimpleContract.sol file with the smart
contract boilerplate. Let’s populate this file with a bit of Solidity code:

1 pragma solidity ^0.4.22;


2
3 contract SimpleContract {
4 string public name;
5
6 constructor() {
7 name = 'my name';
8 }
9
10 function getName() public view returns (string) {
11 return (name);
12 }
13
14 function changeName(string _name) public {
15 name = _name;
16 }
17 }

SimpleContract.sol hosted with ❤ by GitHub view raw

We have just created a SimpleContract which contains a constructor , a getter and a

setter method.

Line 1 specifies the version pragma for Solidity compiler.

Line 3 defines the name of the contract .

Line 4 declares a public string variable name .

Line 6 uses the constructor function, that means this function will be automatically
executed once when the smart contract is created. In this case, the value of the above
https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 8/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

declared variable name will be set to ‘my name’ when the contract is created.

Line 10 is the getter function that will return the value of name. By default, The
compiler automatically creates getter functions for all public state variables.
However, for the sake of understanding, I have just created a getter function here.

Line 14 defines another function to change the value of our name variable.

This is a very simple smart contract with constructor , one getter and one setter

function. In the next step, we will compile this contract.

Compile the Smart Contract


In order to compile the smart contracts, run the following command:

$ truffle compile

This will compile all the smart contracts from the contracts folder and create artifacts

in the contracts folder of the newly created build directory.

1 Compiling .\contracts\Migrations.sol...
2 Compiling .\contracts\SimpleContract.sol...
3
4 Compilation warnings encountered:
5
6 /D/GitHub/truffle-demo/contracts/SimpleContract.sol:6:3: Warning: No visibility specified. Defau
7 constructor() {
8 ^ (Relevant source part starts here and spans across multiple lines).
9
10 Writing artifacts to .\build\contracts

compile.sh hosted with ❤ by GitHub view raw

If we open the contract folder file from the build directory, we can see that these files
are the abstraction of smart contracts and contain the following information:

contract name

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 9/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

ABI (a list of all functions along with their parameter and return values)

bytecode

deployed bytecode

compiler name and version

A list of networks onto which the contract has been deployed (once the contract is
deployed)

address of the contract with respect to each network the contract is deployed to
(once the contract is deployed)

These are .json files and act as a JavaScript wrapper to interact with the respective
smart contracts.

Writing Migrations
In order to deploy smart contract over the blockchain, we first need to write the
configurations for migration in a JavaScript file. These files are basically responsible for
staging the deployment tasks of smart contracts over the blockchain. But, to deploy the
smart contracts with migrations we need to access their artifacts which were created as a
result of truffle compile command.

In order to create a migration file, run the following command in the terminal:

$ truffle create migration deploy_sample_contract

If you go to the migrations folder, you will see a JavaScript migration file is created
which is prefixed by a random number and suffixed by a description. This is because the
history of previously run migrations is recorded on the blockchain and truffle migrate

will start execution from the last run migration. The numbered prefixed is required and
is used to record either a migration has been run successfully or not. Open the newly
created file in text editor and change the contents as follows:

1 const SimpleContract = artifacts.require('SimpleContract');


2
https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 10/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

3 module.exports = function(deployer) {
4 // Use deployer to state migration tasks.
5 deployer.deploy(SimpleContract);
6 };

migrations.js hosted with ❤ by GitHub view raw

In line 1, we are specifying which contract is going to use for interaction using
artifacts.require() method. This method is similar to node.js require method.
However, it will return a contract abstraction that will be used for the rest of the
deployment script. The important thing here is we have to pass the name of the
contract definition and not the name of .sol file. Because a source file may contain
more than one contracts.

At line 3, module.export() is used to export the migrations. This method export a


function which accepts a deployer object as its first parameter. The deployer object
is the main interface for staging the deployment tasks and has many functions
available to simplify migrations. At line 5, we are using deploy method from
deployer in order to deploy our SimpleContract .

After writing the artifacts, make sure that ganache-cli is running in the background and
run:

$ truffle migrate

1 Using network 'development'.


2
3 Running migration: 1_initial_migration.js
4 Deploying Migrations...
5 ... 0xa3995d03ac7b76f0def8714b6989ce68f04166f50aaeb8a8054ae51f546773ab
6 Migrations: 0x6b43963e6d7c07a98f3d4b726d62dcdc0b75ec5a
7 Saving successful migration to network...
8 ... 0x1c8313e68853618011ec0e725b0a83c796c0c1477f7ea5a0e5ce4b25f9a51309
9 Saving artifacts...
10 Running migration: 1542014427_deploy_simple_contract.js
11 Deploying SimpleContract...
12 ... 0xe14804a9f37422ced310b0c18a7b2c860c42be18534def63153e176482bbb20f
13 SimpleContract: 0x4f5c2b6471915398132b627fad0f3089ea5ddd05
14 Saving successful migration to network
https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 11/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium
14 Saving successful migration to network...
15 ... 0x50899b849c640d281a7555a113d816a9512b52cf54cf9c2db6a57c995738f2bf
16 Saving artifacts...

ganache-cli.sh hosted with ❤ by GitHub view raw

Here we can see the address (SimpleContract:


0x4f5c2b6471915398132b627fad0f3089ea5ddd05) where our contract is deployed.

Interacting with the Smart Contract


As we now have the address of our deployed contract, we directly interact it using the
truffle console. Run truffle console to open the console in the terminal:

$ truffle console

in the console type (replace the address with your address)

$ SimpleContract.at('0x4f5c2b6471915398132b627fad0f3089ea5ddd05');

It will return the complete abstraction of our contract. We can get the ABI of the contract

by typing the following command:

$
SimpleContract.at('0x4f5c2b6471915398132b627fad0f3089ea5ddd05').abi;

Similarly, we can call the functions of our contract:

$
SimpleContract.at('0x4f5c2b6471915398132b627fad0f3089ea5ddd05').getNa
me();

now, first run the changeName() function and then run getName() function to see if the
name is changed to the new name or not.
https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 12/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

$
SimpleContract.at('0x4f5c2b6471915398132b627fad0f3089ea5ddd05').chang
eName('your name');
$
SimpleContract.at('0x4f5c2b6471915398132b627fad0f3089ea5ddd05').getNa
me();

When you are done playing with your smart contract, type $ .exit to close the console
mode.

4. Writing Tests
In general, smart contract test can be written both in Solidity and JavaScript. In this
tutorial, We will continue with JavaScript using async/await notation.

If we look at our contact again, we want to test:

1. the constructor is successfully executed while creating the contract. This can be
checked if our getter function returns the same value as passed in the constructor .

2. the changeName method is successfully changing the value of the name variable.

Under the hood, Truffle uses Mocha testing framework and Chai assertion library to test
the smart contracts. Furthermore, truffle test command will run the tests from all the
test files inside the test folder.

But even before writing the tests in JavaScript, let’s write a test to test the constructor in
plain English for our getter method:

“This method should return the name ‘my name’.”


You might have noticed that in order to test a method from a particular contract, we
need to refer to that contract first. We will do that in our actual test by requiring the
artifacts created earlier. Let’s create our test file by typing:

$ truffle create test SimpleContract

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 13/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

Open the newly created simple_contract.js file from the test folder and we will find a
basic test sample there. We can see that Truffle is using contract() instead of
describe() as its main function. The contract() provides a list of account available by
the Ethereum client (in our case Ganache) which can be used to write tests.
Furthermore, it also groups together all the tests for a specific contract. Let’s test the
available accounts. Change the contents of your test file as below and run the following
command:

$ truffle test

1 const SimpleContract = artifacts.require("SimpleContract");


2
3 contract('SimpleContract', (accounts) => {
4 it("should return the list of accounts", async ()=> {
5 console.log(accounts);
6 });
7 });

test-boilerplate.js hosted with ❤ by GitHub view raw

In line 1, we are using artifacts.require() method to request a usable contract


abstraction for a specific Solidity contract.

In line 3, contract() method is used for grouping the tests. It uses a message in
order to understand the purpose of the test.

In line 4, it() is a Mocha notation for a test case.

The actual test starts in line 4, what we are going to test. As a result, we will see a list of
available accounts.

Testing Constructor
Now let’s write the actual test for the constructor :

1 const SimpleContract = artifacts.require("SimpleContract");


2
3 contract('SimpleContract', (accounts) => {
4
https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 14/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium
4
5 it("should return the name", async ()=> {
6 const instance = await SimpleContract.deployed();
7 const value = await instance.getName();
8
9 assert.equal(value, 'my name');
10 });
11 });

getName.js hosted with ❤ by GitHub view raw

In line 6, we have created the instance of our contract to access its methods.

In line 7, we are storing the return value of getName() method so that we can check
it out.

In line 9, we are asserting that the value should be equal to ‘my name’.

Now run the $ truffle test command and we will see one test is passing!

Next, we will add another test for our changeName() method. Add the following test at
line 11 below test 1.

1 it("should return change the name", async ()=> {


2 const instance = await SimpleContract.deployed();
3 await instance.changeName('your name');
4 const value = await instance.getName();
5
6 assert.equal(value, 'your name');
7 });

changeName.js hosted with ❤ by GitHub view raw

In this test we are calling the changeName() method with ‘your name’ and in the next
line, we are storing this value into a variable. Again run the $ truffle test command.

Testing Modifiers
The use of function modifiers in the Solidity is also a common pattern to have a better
control over contract’s methods execution.

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 15/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

Let’s add a modifier onlyOwner() that will restrict the execution of a certain method
only to the owner of the contract.

1 pragma solidity ^0.4.22;


2
3 contract SimpleContract {
4 string public name;
5 address private owner;
6
7 constructor() {
8 name = 'my name';
9 owner = msg.sender;
10 }
11
12 modifier onlyOwner() {
13 require(msg.sender == owner);
14
15 _;
16 }
17
18 function getName() public view returns (string) {
19 return (name);
20 }
21
22 function changeName(string _name) public onlyOwner {
23 name = _name;
24 }
25 }

SimpleContract-Modifier.sol hosted with ❤ by GitHub view raw

We have created a private variable owner and assign it to the creator of the contract
using the constructor function.

In line 12, we have declared a function modifier which will check if the caller of the
method is the owner of the contract, then continue ( _; ).

In line 22, the function modifier is assigned to the method changeName() by adding
the name of the modifier.

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 16/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

Open the simple_contract.js file again and add the following test for the modifier. But
before doing that, we will re-arrange the test to make our lives even easier.

1 const SimpleContract = artifacts.require("SimpleContract");


2
3 contract('SimpleContract', (accounts) => {
4
5 let instance;
6 beforeEach('should setup the contract instance', async () => {
7 instance = await SimpleContract.deployed();
8 });
9
10 it("should return the name", async ()=> {
11 const value = await instance.getName();
12
13 assert.equal(value, 'my name');
14 });
15
16 it("should return change the name", async ()=> {
17 await instance.changeName('your name');
18 const value = await instance.getName();
19
20 assert.equal(value, 'your name');
21 });
22
23 it('should execute only by the owner', async()=>{
24 await instance.changeName('modifier');
25 const value = await instance.getName();
26
27 assert.equal(value, 'modifier');
28 })
29
30 });

beforeEach.js hosted with ❤ by GitHub view raw

Instead of creating the contract instance for every test, we have created a global instance
of the contract using the beforeEach hook provided by Mocha.

As we have put the modifier for changeName() method, now only the owner should be
able to change the name. This test is specified at line 23. If we run the $ truffle test

command again, we see that the test is passed. This is because if we don’t specify the
https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 17/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

account, ganache automatically uses the first account. In this case, the owner of the
contract and caller of the method are the same, so why the test is passed. In order to
really check the modifier, let’s use the transaction object in our test:

1 it('should execute only by the owner', async()=>{


2 await instance.changeName('modifier', {'from': accounts[1]});
3 const value = await instance.getName();
4
5 assert.equal(value, 'modifier');
6 })

modifier-test.js hosted with ❤ by GitHub view raw

Use any other account other than the first account ( accounts[0] ) from the list of

available accounts and run the test again. You will see your test is failed now because the
only owner can call this method. Although, using any other account other than the
owner will lead to failing the test but we want our test to pass. This is important, because
although the test is asserting a failing behavior, a test itself should always pass its
assertion. Here comes the idea of revert into play. The truffle-assertions library has
the ability to assert that a transaction reverts as expected. The truffle-assertions

package adds additional assertions that can be used to revert transactions in the smart
contracts.

Install the package by running the following command in the terminal:

$ npm install truffle-assertions

Also import this package at the top or our test file, just below the liner requiring artifacts
of the contract:

const truffleAssert = require('truffle-assertions');

add the following test and run the truffle test command again. At this time our test is
passed with the expected behavior of the contract!

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 18/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

1 it('should fail', async () => {


2 await truffleAssert.reverts(instance.changeName('modifier', {
3 'from': accounts[1]
4 }));
5 });

fail-test.js hosted with ❤ by GitHub view raw

Listening Events
Using the truffle-assertions package that we just installed, we can:

1. check the event type

2. check events parameters

3. print the event

Let’s add a name event that will be fired every time name is changed.

event NameEvent(string evPram);

now emit this event in the changeName() function as below:

1 function changeName(string _name) public onlyOwner {


2 name = _name;
3 emit NameEvent(name);
4 }

eventType.js hosted with ❤ by GitHub view raw

add the following test to check if a specific event is present in the transaction or not and
run the truffle test command:

1 it('should check the type of the event', async () => {


2 const result = await instance.changeName('hello event');
3 truffleAssert.eventEmitted(result, 'NameEvent');
4 });

checkEvent.js hosted with ❤ by GitHub view raw

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 19/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

We are saving the transaction into a result variable and then check that if ‘NameEvent’ is
emitted by the transaction or not.

Now we will check if the event is emitted with correct parameters:

1 it('should emit with correct paremeters', async()=>{


2 const result = await instance.changeName('hello event');
3 truffleAssert.eventEmitted(result, 'NameEvent', (event) =>{
4 return event.evPram == 'hello event';
5 });
6 });

eventPrameter.js hosted with ❤ by GitHub view raw

In the end, we are going to print out the event parameters and their values emitted by a
particular event:

1 it('should print the event paremeters', async()=>{


2 let result = await instance.changeName('hello event');
3 truffleAssert.prettyPrintEmittedEvents(result);
4 })

printEvent.js hosted with ❤ by GitHub view raw

after running the truffle test command, you will see the transaction emitting the event,
name of the emitted event, its parameter, and values.

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 20/21
9/4/2019 Test Driven Solidity with Truffle - OLI Systems - Medium

The source code can be found here in this GitHub repo.

Related articles
Truffle
Truffle Assertions
Mocha
Chai

Tru e Smart Contracts Unit Testing

About Help Legal

https://medium.com/oli-systems/test-driven-solidity-with-truffle-e4beaa2bd194 21/21

Вам также может понравиться