Since 2009, we have been utilizing our extensive expertise in blockchain technologies to help businesses, both large and small, maximize their efficiency.
Explore More
With more than 400+ experts, Oodles comprises a fantastic resource of business knowledge that spans multiple industries. Whatever the circumstances, we keep to our obligations.
Explore More
At Oodles, we help our clients work with a human understanding but at superhuman speed something that others can't. They thus advance and maintain their lead
31st October 2023
10 min read
Sr. Associate Consultant L2 - Development
Smart contracts have become a fundamental building block of decentralized applications (DApps) on blockchain platforms like Ethereum. After smart contract development, rigorous testing is essential to ensure the security and reliability of these contracts. In this blog post, we will learn about the tools and how to test a smart contract.
Before starting with the testing, it is important to understand various types of testing and tools and frameworks used to test smart contracts for Ethereum.
There are several types of testing for Solidity smart contracts, each serving a specific purpose:
Several tools and frameworks are available to help test Solidity smart contracts:
Suggested Post | Analyzing Solidity and Vyper for Smart Contracts Programming
In this section, we will test an ERC20 smart contract using the Hardhat environment. Below are the test cases for a standard ERC20 smart contract:
The "beforeEach" function deploys a new smart contract in the test environment for every "it" block in the test cases. Each "it" block has a description that denotes the functionality that the block tests.
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Token contract", function () {
let Token;
let token;
let owner;
let buyer;
beforeEach(async function () {
Token = await ethers.getContractFactory("MyToken");
token = await Token.deploy(1000);
[owner, buyer] = await ethers.getSigners();
});
describe("Deployment", function () {
it("Should return the right owner as set during deployment", async function () {
expect(await token.balanceOf(owner.address)).to.equal(1000);
});
it("Should return the total supply as set during deployment", async function () {
expect(await token.totalSupply()).to.equal(1000);
});
});
describe("Transactions", function () {
it("Should transfer tokens between different accounts", async function () {
await token.transfer(buyer.address, 100);
expect(await token.balanceOf(owner.address)).to.equal(900);
expect(await token.balanceOf(buyer.address)).to.equal(100);
});
it("Should fail if sender doesn't have enough tokens for transfer", async function () {
const initialOwnerBalance = await token.balanceOf(owner.address);
await expect(
token.transfer(buyer.address, 10000)
).to.be.revertedWithoutReason();
expect(await token.balanceOf(owner.address)).to.equal(initialOwnerBalance);
});
it("Should update allowance after approve", async function () {
await token.approve(buyer.address, 100);
expect(await token.allowance(owner.address, buyer.address)).to.equal(100);
});
it("Should transfer tokens from one account to another with allowance", async function () {
await token.approve(buyer.address, 100);
await token.transferFrom(owner.address, buyer.address, 100);
expect(await token.balanceOf(owner.address)).to.equal(900);
expect(await token.balanceOf(buyer.address)).to.equal(100);
expect(await token.allowance(owner.address, buyer.address)).to.equal(0);
});
it("Should fail if sender doesn't have enough allowance", async function () {
await token.approve(buyer.address, 99);
await expect(
token.transferFrom(owner.address, buyer.address, 100)
).to.be.revertedWith("ERC20: transfer amount exceeds allowance");
});
});
});
Also, Discover: A Definitive Guide to Smart Contract Development Tools
Smart contracts developers at Oodles Blockchain have expertise in programming languages like Solidity, Elixir, Rust, Golang, and more. you can automate your business with our smart contract development services. Connect with our smart contract developers to discuss your project requirements.
Mohd Yasar
Mohd Yasar is a skilled Backend Developer, with proficiency in NodeJs, MongoDB, MySql, Docker, Solidity, NFT's, and microservices architecture. He has extensive experience in developing secure and scalable solutions for complex applications, and has delivered successful projects such as Scaffold, Cryptomining, Mintlab, WalletPort, Data Management in Distributed Systems, and Bluechain, meeting the clients' requirements. Overall, he seems to have a diverse skill set and a strong foundation in developing scalable and reliable projects using a variety of technologies.
Sr. Associate Consultant L2 - Development
By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.
We would love to
hear from you!
Innovate with confidence!