This project was perform for Alya Solidity training. It demonstrates how to implement a javascript unit test for that contract.
Explore the docs Β»
Report Bug
Β·
Request Feature
Table of Contents
The objective of the exercise is to provide the unit tests of a smart contract including everts and revert.
the contract to be tested is based on a previous project. See below the repo link :
In this context, we have created three test files: :
-
Ownable.test.js
: to test if Ownable from openzepplin is correctly implementedUnits tests on Ownable access functions π Test Ownable Revert if the owner is not the caller β should NOT start Proposal session registering if caller is not the owner (58ms) β should NOT end Proposal session registering if caller is not the owner (46ms) β should NOT start voting session if caller is not the owner (50ms) β should NOT end voting session if caller is not the owner (60ms) β should NOT start tallyVotes if caller is not the owner (56ms)
-
Voting.test.js
: to test the each function of Voting smart contract separatelyUnits tests of each function of voting smart contract π Deployment hardhat deploy testing β should deploy the smart contract π Get add new voters function unit test β should emit event VoterRegistered β should be able to set owner as a voter β should add a new voter_1 and change bool isRegistered from false to true (40ms) β should NOT add a new voter and revert caller is not the owner π Get voters function unit test β should return true for _owner and false for voter_1 not yet Registered β should revert as voter is not registered π AddVoter failed as voter registration is not open yet β should NOT addVoter and revert with Voters registration is not open yet (95ms) β should NOT addVoter if not the owner π Get add new proposal function unit test β should add a new proposal β should emit event add proposal π AddProposals failed as Proposals are not allowed yet β should NOT addProposals and revert with Proposals are not allowed yet (109ms) β should NOT addProposal if caller is not a Voter π getOneProposal function unit test β should NOT give proposal if not Voter (48ms) π Get add new vote function unit test β should add a new vote and increment voteCount π Test revert setVote function unit test β should revert setVote as the voting session not started (92ms) β should revert as caller is not a voter (60ms) β should revert with Voting session havent started yet (73ms) β should revert with Proposal not found (47ms) π Get end voting session function unit test β should end the voting session (81ms) π TallyVotes function unit test β should return the winning proposal β should change the status π TallyVotes revert function unit test β should NOT work if not votingSessionEnded β should NOT work if not Owner (49ms)
-
VotingWorkflow.test.js
: to test the overall workflow of the Voting contractUnits tests for overall workflow of voting smart contract π Control workflow status not allow to change if previous status is NOT correct β should NOT start Proposal registering if the workflow status is not the expected one (67ms) β should NOT end Proposal registering if the workflow status is not the expected one (63ms) β should NOT start voting session in incorrect WF (62ms) β should NOT end voting session if the workflow status is not the expected one (58ms) β should NOT launch tallyVotes (65ms) π Check event of each workflow status change β should start Proposal registering if the owner β should end Proposal registering if the owner β should start voting session if the owner β should end voting session if the owner β should launch tallyVotes if the owner (45ms) ```
Below the coverage report of the test :
-------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
-------------|----------|----------|----------|----------|----------------|
contracts/ | 100 | 93.75 | 100 | 100 | |
Voting.sol | 100 | 93.75 | 100 | 100 | |
-------------|----------|----------|----------|----------|----------------|
All files | 100 | 93.75 | 100 | 100 | |
-------------|----------|----------|----------|----------|----------------|
Below the instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.
Here the list things you need to use the software and how to install them.
- For this project we are going to use yarn
- installation
npm install --global yarn
- check installation
yarn --version
- solidity
_Below instruction installing and setting up the project.
-
Clone the repo
git clone https://github.com/BksRhalid/Alyra-VotingSystem_including_UnitTest.git
-
Install packages
yarn install
-
Enter your Wallet Private Key and API in
env
const PK = "YOUR WALLET PRIVATE KEY";
const INFURA = "ENTER YOUR API";
const ETHERSCAN = "ENTER YOUR API";
-
Check dependencies used like Openzepplin contracts / hardhat-deploy
yarn add --dev @openzeppelin/contracts yarn add --dev hardhat-deploy
In case of error: _TypeError: ethers.getContract is not a function
see reference : https://ethereum.stackexchange.com/questions/130125/solvedpatrickcollins-solidity-course-2022-lesson-7-112038-typeerror-e
yarn add --dev hardhat @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers
Try running some of the following tasks:
REPORT_GAS=true npx hardhat test
Distributed under the MIT License.