{"id":23868413,"url":"https://github.com/powerstream3604/polygon-unit-testing","last_synced_at":"2026-06-20T17:32:06.289Z","repository":{"id":181125713,"uuid":"419791052","full_name":"PowerStream3604/Polygon-unit-testing","owner":"PowerStream3604","description":"Github repo of tutorial on FigmentLearn about unit-testing with truffle on polygon","archived":false,"fork":false,"pushed_at":"2021-11-01T04:38:33.000Z","size":130,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-12T17:03:22.072Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PowerStream3604.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-10-21T16:06:47.000Z","updated_at":"2022-11-16T09:31:41.000Z","dependencies_parsed_at":"2023-07-14T06:37:45.039Z","dependency_job_id":null,"html_url":"https://github.com/PowerStream3604/Polygon-unit-testing","commit_stats":null,"previous_names":["powerstream3604/polygon-unit-testing"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/PowerStream3604/Polygon-unit-testing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PowerStream3604%2FPolygon-unit-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PowerStream3604%2FPolygon-unit-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PowerStream3604%2FPolygon-unit-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PowerStream3604%2FPolygon-unit-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PowerStream3604","download_url":"https://codeload.github.com/PowerStream3604/Polygon-unit-testing/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PowerStream3604%2FPolygon-unit-testing/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34580039,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-03T11:28:13.122Z","updated_at":"2026-06-20T17:32:06.273Z","avatar_url":"https://github.com/PowerStream3604.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Polygon Smart Contract unit testing with truffle\n\n# Introduction\nIn this tutorial, you will learn how to deploy \u0026 unit test smart contracts in solidity using Truffle. Before diving into unit testing we will implement a smart contract for testing. Grab a cup of tea, review the Smart-Contract and unit test them!\n\n# Prerequisites\nThis tutorial assumes that you have a basic understanding of Solidity, Truffle, and blockchains.\n\n# Requirements\n- [Truffle](https://www.trufflesuite.com/)\n- The [Solidity](https://docs.soliditylang.org/en/v0.8.9/) compiler (this is installed automatically by Truffle)\n- [Metamask](https://metamask.io/)\n\n# Deep Dive into unit testing on polygon\n\n## Github Repository\nThe complete code used in this tutorial is available [on Github](https://github.com/PowerStream3604/Polygon-unit-testing)\n\n**What is unit testing?**\nUnit testing is a way of **testing a unit** - the smallest piece of code that can be logically isolated in a system. These units are mostly functions, subroutine, methods or properties.\n\n## Introduction about the Smart Contract we'll implement.\nBefore we can test a smart contract, we will need to implement one.\n\nThe smart contract we'll implement is a Smart Contract that represents a **Corportion**. Similar to how corporations operate, our smart contract assign **roles:**\n**Owners :** Shareholders of the company who have limited access.\n**Master :** The president/CEO of the company with full access.\n**Admins :** Both **Owners** and **Master** together as a group.\n\n## Features :\n1. **Master** has the right to **add** owner with **addOwner()** and **remove** owner with **removeOwner()**.\n2. **Admins** (owner \u0026 master) have the right to transfer their share to anyone using the **giveShare()** function.\n3. **Admins** (owner \u0026 master) have the right to transfer their share to one of the owners(not master) using the **addShare()** function.\n4. **checkIfOwner()** returns whether the given address is owner or not.\n5. **getMaster()** returns the address of the **master**.\n6. **getOwners()** returns the list of **owner** addresses.\n7. **Users** who are not in the boundary of **Admins** cannot transfer their share but can still **receive**.\n\n\n## Events :\n1. **MasterSetup** is emitted when **Master** is set\n\n```solidity\nevent MasterSetup(address indexed master);\n```\n\n2. **OwnerAddition** is emitted when **Owner** is added by **Master**\n\n```solidity\nevent OwnerAddition(address indexed owner);\n```\n\n3. **OwnerRemoval** is emitted when **Owner** is removed by **Master**\n\n```solidity\nevent OwnerRemoval(address indexed owner);\n```\n\n4. **Transfer** is emitted when share of admins gets transfer by either of these functions **addShare()**, **giveShare()**\n\n```solidity\nevent Transfer(address indexed receiver, uint256 amount);\n```\n\n# Unit testing smart contracts with Truffle\n\n## Defining the smart contract\n\n*Notes: We name this smart Contract as **Company** and use solidity version **0.8.7***\n\nBefore defining the events and functions, let's first define variables to be used in the smart contract.\n\n```solidity\ncontract Company {\n    \n    // Company contract.\n    // owners of the contract are the share holders of the company.\n    \n    address public master;\n    // List to keep track of all owner's address\n    address[] public owners;\n    // Mapping to keep track of all owners of the company\n    mapping(address =\u003e bool) isOwner;\n    // Mapping to keep track of all balance of share holders of the company\n    mapping(address =\u003e uint256) share;\n    \n}\n```\n\nAfter defining the variables, we should define events to be used in the Company Smart Contract.\n\n```solidity\n// Events\nevent MasterSetup(address indexed master);\nevent OwnerAddition(address indexed owner);\nevent Transfer(address indexed receiver, uint256 amount);\nevent OwnerRemoval(address indexed owner);\n```\n\nSince we defined the events, we should define modifier to limit access from **anonymous** or **unauthorized** users.\n\n```solidity\nmodifier onlyMaster() {\n    require(msg.sender == address(master));\n    _;\n}\nmodifier onlyOwners() {\n    require(isOwner[msg.sender], \"Only owners have the right to call\");\n    _;\n}\nmodifier onlyAdmins() {\n    require(isOwner[msg.sender] || msg.sender == address(master), \"Only master or owners have the right to call\");\n    _;\n}\n```\n\nWe should define the constructor to set the address of the **Master**.\n\n```solidity\n/// @dev Constructor sets the master address of Company contract.\n/// @param _master address to setup master \nconstructor(address _master) {\n    require(_master != address(0), \"Master address cannot be a zero address\");\n    master = _master;\n    share[master] = 10000000;\n    emit MasterSetup(master);\n}\n```\n\nAlso, we should define functions to get the information about **Master** and **Owners**.\n\n```solidity\n/// @dev Returns the address of the master\nfunction getMaster()\n    public\n    view\n    returns (address)\n{\n    return master;\n}\n\n/// @dev Returns the owner list of this Company contract.\nfunction getOwners()\n    public\n    view\n    returns (address[] memory)\n{\n    return owners;\n}\n```\n\nWe would need a function to check if the given address is owner or not.\n\n```solidity\n/// @dev Returns whether the given address is owner or not.\n/// @param owner Address to check if is owner.\nfunction checkIfOwner(address owner)\n    public\n    view\n    returns (bool)\n{\n    return isOwner[owner];\n}\n```\n\nWe would also need functions to **add** and **remove** owners.\n\n```solidity\n/// @dev Adds owner if the msg.sender is master. Will revert otherwise.\n/// @param owner Owner address to be added as owner.\nfunction addOwner(address owner) \n    onlyMaster \n    public\n{\n    isOwner[owner] = true;\n    owners.push(owner);\n    share[owner] = 5000000;\n    \n    emit OwnerAddition(owner);\n}\n\n/// @dev Removes an owner from the owner list. Can only be called by master. Will Revert otherwise\n/// @param owner Address of the owner to be removed\nfunction removeOwner(address owner)\n    public\n    onlyMaster\n{\n    require(isOwner[owner], \"Only owners can be removed from owner list\");\n    isOwner[owner] = false;\n    for (uint i = 0; i \u003c owners.length - 1; i++)\n        if (owners[i] == owner) {\n            owners[i] = owners[owners.length - 1];\n            break;\n        }\n    owners.pop();\n        \n    emit OwnerRemoval(owner);\n}\n```\n\nThe most important part of all, we need functions to transfer share.\n\n```solidity\n/// @dev Transfers owner's or master's share to any address given.\n///     Note: can only be called by one of the owners or master\n/// @param receiver Address of the receiver who'll receive the share\n/// @param _share Uint256 of the amount the admin wants to transfer\nfunction giveShare(address receiver, uint256 _share)\n    public\n    onlyAdmins\n{\n    require(share[msg.sender] \u003e= _share, \"Share exceeds the sender allowance\");\n    share[msg.sender] = share[msg.sender] - _share;\n    share[receiver] += _share;\n        \n    emit Transfer(receiver, _share);\n}\n    \n/// @dev Transfers owner's or master's stake(share) to an address in the owner list or master.\n///     Note: the recipient can only be one of the admins(owner or master)\n/// @param receiver Address of the receipient\n/// @param _share Uint256 amount of the stake(share) to transfer\nfunction addShare(address receiver, uint256 _share)\n    public\n    onlyAdmins\n{\n    require(share[msg.sender] \u003e= _share, \"Share exceeds the sender allowance\");\n    require(isOwner[receiver], \"The receipient should only be one of the owners\");\n    share[msg.sender] -= _share;\n    share[receiver] += _share;\n        \n    emit Transfer(receiver, _share);\n}\n```\n\nHere is the full **implementation**.\n\n```solidity\n// SPDX-License-Identifier: MIT\npragma solidity ^0.8.9;\n\n    \ncontract Company {\n    \n    // Company contract.\n    // owners of the contract are the share holders of the company.\n\n    \n    address public master;\n    // List to keep track of all owner's address\n    address[] public owners;\n    // Mapping to keep track of all owners of the company\n    mapping(address =\u003e bool) isOwner;\n    // Mapping to keep track of all balance of share holders of the company\n    mapping(address =\u003e uint256) share;\n    \n    \n    // Events\n    event MasterSetup(address indexed master);\n    event OwnerAddition(address indexed owner);\n    event Transfer(address indexed receiver, uint256 amount);\n    event OwnerRemoval(address indexed owner);\n    \n    \n    modifier onlyMaster() {\n        require(msg.sender == address(master));\n        _;\n    }\n    modifier onlyOwners() {\n        require(isOwner[msg.sender], \"Only owner can be added\");\n        _;\n    }\n    modifier onlyAdmins() {\n        require(isOwner[msg.sender] || msg.sender == address(master), \"Only master or owners\");\n        _;\n    }\n    \n    /// @dev Constructor sets the master address of Company contract.\n    /// @param _master address to setup master \n    constructor(address _master) {\n        require(_master != address(0), \"Master address cannot be a zero address\");\n        master = _master;\n        share[master] = 10000000;\n        emit MasterSetup(master);\n    }\n    \n    /// @dev Returns the address of the master\n    function getMaster()\n        public\n        view\n        returns (address)\n    {\n        return master;\n    }\n    \n    /// @dev Adds owner if the msg.sender is master. Will revert otherwise.\n    /// @param owner Owner address to be added as owner.\n    function addOwner(address owner) \n        onlyMaster \n        public\n    {\n        isOwner[owner] = true;\n        owners.push(owner);\n        share[owner] = 5000000;\n        \n        emit OwnerAddition(owner);\n    }\n    \n    /// @dev Returns the owner list of this Company contract.\n    function getOwners()\n        public\n        view\n        returns (address[] memory)\n    {\n        return owners;\n    }\n    \n    /// @dev Returns whether the given address is owner or not.\n    /// @param owner Address to check if is owner.\n    function checkIfOwner(address owner)\n        public\n        view\n        returns (bool)\n    {\n        return isOwner[owner];\n    }\n\n    /// @dev Transfers owner's or master's share to any address given.\n    ///     Note: can only be called by one of the owners or master\n    /// @param receiver Address of the receiver who'll receive the share\n    /// @param _share Uint256 of the amount the admin wants to transfer\n    function giveShare(address receiver, uint256 _share)\n        public\n        onlyAdmins\n    {\n        require(share[msg.sender] \u003e= _share, \"Stake exceeds the sender allowance\");\n        share[msg.sender] = share[msg.sender] - _share;\n        share[receiver] += _share;\n        \n        emit Transfer(receiver, _share);\n    }\n    \n    /// @dev Transfers owner's or master's stake(share) to an address in the owner list or master.\n    ///     Note: the recipient can only be one of the admins(owner or master)\n    /// @param receiver Address of the receipient\n    /// @param _share Uint256 amount of the stake(share) to transfer\n    function addShare(address receiver, uint256 _share)\n        public\n        onlyAdmins\n    {\n        require(share[msg.sender] \u003e= _share, \"Share exceeds the sender allowance\");\n        require(isOwner[receiver], \"The receipient should only be one of the owners\");\n        share[msg.sender] = share[msg.sender] - _share;\n        share[receiver] += _share;\n        \n        emit Transfer(receiver, _share);\n    }\n    \n    /// @dev Removes an owner from the owner list. Can only be called by master. Will Revert otherwise\n    /// @param owner Address of the owner to be removed\n    function removeOwner(address owner)\n        public\n        onlyMaster\n    {\n        require(isOwner[owner], \"Only owners can be removed from owner list\");\n        isOwner[owner] = false;\n        for (uint i = 0; i \u003c owners.length - 1; i++)\n            if (owners[i] == owner) {\n                owners[i] = owners[owners.length - 1];\n                break;\n            }\n        owners.pop();\n        \n        emit OwnerRemoval(owner);\n    }\n}\n```\n\n**Hooray!!** We implemented the whole contract!!\n\nLet's then go to unit test the above contract with Truffle.\n\n## Unit Testing With Truffle\n\nAs I mentioned above, unit testing is testing the **smallest** unit which can be functions, subroutines, methods, etc.\nTruffle provides a convinient library to test smart contracts, by using **truffle-assert** library, we'll check if all scenarios stand by our expectation.\n\n## Initialize truffle project\n\n```text\ntruffle init\n```\n\nThen, you'll see a project directory like this.\n\n![project overview](https://i.ibb.co/19Xnw0X/Screen-Shot-2021-10-22-at-9-52-29-AM.png)\n\n## Paste your smart contract into the contracts folder\n\nCreate Company.sol file under `/contracts`\n\n```text\ntouch Company.sol\n```\n\n**Then**, paste the smart contract.\n\n## Configure the network settings\n\n**Edit** the configuration file:\n```text\ntruffle-config.js\n```\n\nInside the **networks** object paste the below network configuration.\n\n```javascript\nmumbai: {\n      provider: () =\u003e new HDWalletProvider([\"\u003cPrivate Key 1\u003e\", \"\u003cPrivate Key 2\u003e\", \"\u003cPrivate Key 3\u003e\"],\n      \"https://polygon-mumbai.infura.io/v3/[PROJECT-ID]\"),\n      network_id: 80001,\n      confirmations: 2,\n      timeoutBlocks: 200,\n      skipDryRun: true,\n      networkCheckTimeout: 100000,\n}\n```\n\nthe above configuration sets the provider url to connect truffle with the node of **Mumbai testnet**, and provides private keys to **sign** and pay for **gas fee** on **Mumbai**.\n\nWe need **3 distinct private keys** for this test. You can refer to this manual to export private key from [Metamask](https://metamask.zendesk.com/hc/en-us/articles/360015289632-How-to-Export-an-Account-Private-Key).\n\nFor creating Infura projects, please refer this [Infura manual](https://blog.infura.io/getting-started-with-infura-28e41844cc89/).\n\n*NOTES : The accounts should be funded with **MATIC** on Mumbai. Use the [Polygon faucet](https://faucet.polygon.technology/)*\n\n## Create company.js file in test folder\n\nIn order to create test in truffle, create a test file under the `test` folder.\n\n```text\ncd test\ntouch company.js\n```\n\n## Create deployment file to deploy Company Contract\n\n```text\ncd migrations\ntouch 2_deploy_contract.js\n```\n\nTo deploy a contract, we will use a migration script (migration is Truffle's way of saying deployment). \nAdd the following code to `2_deploy_contract.js`:\n\n```javascript\nconst Company = artifacts.require(\"Company\");\n\nmodule.exports = function (deployer, networks, accounts) {\n    deployer.deploy(Company, account);\n};\n```\n\n## Before unit testing smart contract\nWe'll use the javascript library of truffle to test the functions.\n\nBefore going in, we'll import the contract we'll test and the truffle library for testing.\n\n```javascript\nconst Company = artifacts.require(\"Company\");\nconst truffleAssert = require('truffle-assertions');\n```\n\nAlso, we'll define user variable to better distinguish accounts for testing we designated in the **truffle-config.js** file.\n\n```javascript\nconst user1 = accounts[0];\nconst user2 = accounts[1];\nconst user3 = accounts[2];\n```\n\n## Writing unit tests\n\n1. Test if the Master address is set appropariately by the constructor.\n\n```javascript\nit(\"1. should be able to set the right master\", async () =\u003e {\n    // Get deployed contract\n    const company = await Company.deployed();\n    // Check if the master address equals to user1\n    assert.deepEqual(await company.getMaster(), user1);\n});\n```\n\n2. Check if only master is able to add owner\n\n```javascript\nit(\"2. only master should be able to add owner\", async () =\u003e {\n    // Get deployed contract\n    const company = await Company.deployed();\n    // Check if the user2 (is not master) gets reverted when attempting to add owner\n    await truffleAssert.reverts(\n        company.addOwner(user1, {from: user2}),\n    );\n});\n```\n\n3. Check if Master is able to add owner.\n\n```javascript\nit(\"3. master should be able to add owner\", async() =\u003e {\n    // Get deployed contract\n    const company = await Company.deployed();\n    // call addOwner(); {from: accounts[0]} is added as default (who is master)\n    const tx = await company.addOwner(user1);\n    // Check if OwnerAddition is getting emitted\n    truffleAssert.eventEmitted(tx, \"OwnerAddition\", (ev) =\u003e {\n        return ev.owner == user1;\n    })\n    // Check if getOwners() returns the ownerList with user1 as owner inside\n    assert.deepEqual(await company.getOwners(), [user1]);\n});\n```\n\n4. Check if an address is an owner\n\n```javascript\nit(\"4. should be able to check owner\", async () =\u003e {\n    const company = await Company.deployed();\n    await company.addOwner(user1);\n\n    // checkIfOwner() should true since user1 is in the ownerList   \n    assert.equal(await company.checkIfOwner(user1), true);\n});\n```\n\n5. Check if master is the only account to remove owner\n\n```javascript\nit(\"5. only master should be able to remove owners\", async () =\u003e {\n    const company = await Company.deployed();\n\n    await truffleAssert.reverts(\n        company.removeOwner(user1, {from: user2}),\n    );\n});\n```\n\n6. Check if Master is able to add owner\n\n```javascript\nit(\"6. master should be able to remove owners\", async () =\u003e {\n    const company = await Company.deployed();\n\n    const tx = await company.removeOwner(user1);\n    // check if OwnerRemoval event is emitted\n    truffleAssert.eventEmitted(tx, \"OwnerRemoval\", (ev) =\u003e {\n        return ev.owner == user1;\n    });\n    // ownerList should be empty\n    assert.deepEqual(await company.getOwners(), []);\n});\n```\n\n7. Check if Master is able to send his share to owners\n\n```javascript\nit(\"7. master should be able to send his share to owners\", async() =\u003e {\n    const company = await Company.deployed();\n    // add user2 as owner\n    await company.addOwner(user2);\n\n    // the initial share is 5000000\n    assert.equal(await company.getShare(user2), 5000000);\n\n    // add the share of master to user2\n    const tx1 = await company.addShare(user2, 1000);\n\n    // check if Transfer event is getting emitted\n    truffleAssert.eventEmitted(tx1, \"Transfer\", (ev) =\u003e {\n        return ev.receiver == user2 \u0026\u0026 ev.amount == 1000\n    });\n    // get the share of user2\n    const user2Share = await company.getShare(user2);\n\n    // user2 share should be 5000000 + 1000 : 5001000\n    assert.equal(user2Share.toString(), 5001000);\n});\n```\n\n8. Check if it's not possible to user `addShare()` to transfer share to ordinary users\n\n```javascript\nit(\"8. should not be able to use addShare() to transfer share to non-admins(normal-users)\", async() =\u003e {\n    const company = await Company.deployed();\n\n    await truffleAssert.reverts(\n        company.addShare(user3, 1000),\n    );\n});\n```\n\n9. Check if it's possible to use `giveShare()` to transfer share to ordinary users\n\n```javascript\nit(\"9. should be able to use giveShare() to transfer share to anyone\", async () =\u003e {\n    const company = await Company.deployed();\n\n    const tx = await company.giveShare(user3,  1500);\n    truffleAssert.eventEmitted(tx, \"Transfer\", (ev) =\u003e {\n        return ev.receiver == user3 \u0026\u0026 ev.amount == 1500;\n    });\n\n    assert.equal(await company.getShare(user3), 1500);\n});\n```\n\n## The whole test code\n\n```javascript\nconst Company = artifacts.require(\"Company\");\nconst truffleAssert = require(\"truffle-assertions\");\ncontract(\"Company\", (accounts) =\u003e {\n  const user1 = accounts[0];\n  const user2 = accounts[1];\n  const user3 = accounts[2];\n  console.log(accounts);\n\n  it(\"1. should be able to set the right master\", async () =\u003e {\n    const company = await Company.deployed();\n    assert.deepEqual(await company.getMaster(), user1);\n  });\n\n  it(\"2. only master should be able to add owner\", async () =\u003e {\n    const company = await Company.deployed();\n    await truffleAssert.reverts(company.addOwner(user1, { from: user2 }));\n  });\n\n  it(\"3. master should be able to add owner\", async () =\u003e {\n    const company = await Company.deployed();\n    const tx = await company.addOwner(user1);\n    truffleAssert.eventEmitted(tx, \"OwnerAddition\", (ev) =\u003e {\n      return ev.owner == user1;\n    });\n\n    assert.deepEqual(await company.getOwners(), [user1]);\n  });\n\n  it(\"4. should be able to check owner\", async () =\u003e {\n    const company = await Company.deployed();\n    await company.addOwner(user1);\n\n    assert.equal(await company.checkIfOwner(user1), true);\n  });\n\n  it(\"5. only master should be able to remove owners\", async () =\u003e {\n    const company = await Company.deployed();\n\n    await truffleAssert.reverts(company.removeOwner(user1, { from: user2 }));\n  });\n\n  it(\"6. master should be able to remove owners\", async () =\u003e {\n    const company = await Company.deployed();\n\n    const tx = await company.removeOwner(user1);\n    truffleAssert.eventEmitted(tx, \"OwnerRemoval\", (ev) =\u003e {\n      return ev.owner == user1;\n    });\n    assert.deepEqual(await company.getOwners(), []);\n  });\n\n  it(\"7. master should be able to send his share to owners\", async () =\u003e {\n    const company = await Company.deployed();\n\n    await company.addOwner(user2);\n\n    assert.equal(await company.getShare(user2), 5000000);\n\n    const tx1 = await company.addShare(user2, 1000);\n    truffleAssert.eventEmitted(tx1, \"Transfer\", (ev) =\u003e {\n      return ev.receiver == user2 \u0026\u0026 ev.amount == 1000;\n    });\n\n    const user2Share = await company.getShare(user2);\n\n    assert.equal(user2Share.toString(), 5001000);\n  });\n\n  it(\"8. should not be able to use addShare() to transfer share to non-admins(normal-users)\", async () =\u003e {\n    const company = await Company.deployed();\n\n    await truffleAssert.reverts(company.addShare(user3, 1000));\n  });\n\n  it(\"9. should be able to use giveShare() to transfer share to anyone\", async () =\u003e {\n    const company = await Company.deployed();\n\n    const tx = await company.giveShare(user3, 1500);\n    truffleAssert.eventEmitted(tx, \"Transfer\", (ev) =\u003e {\n      return ev.receiver == user3 \u0026\u0026 ev.amount == 1500;\n    });\n\n    assert.equal(await company.getShare(user3), 1500);\n  });\n});\n```\n\n## Run test\n\n```text\ntruffle test --network mumbai\n```\n\n## Test Result\n![Test Result](https://i.ibb.co/Fqs3vjb/Screen-Shot-2021-10-22-at-9-24-47-AM.png)\n\n\n# Conclusion\nAfter reading this tutorial you'll able to :\n- Write a basic smart contract with solidity\n- Use the Truffle library to unit test smart contracts\n\n# About the Author\nDavid Kim : BlockChain developer with much interest in NFT, DEFI.\n\nGithub : [GitHub](https://github.com/PowerStream3604)\n\n# References\n- [Truffle](https://www.trufflesuite.com/)\n- [Solidity](https://docs.soliditylang.org/en/v0.8.9/)\n- [Polygon docs](https://docs.matic.network/docs/develop/getting-started)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpowerstream3604%2Fpolygon-unit-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpowerstream3604%2Fpolygon-unit-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpowerstream3604%2Fpolygon-unit-testing/lists"}