How to create tests for your Solidity Smart Contract

Why should you bother to write tests?

🛠 Tools you should use when developing

What should you cover in tests?

Cover Known bugs/exploits/common errors

Cover the implementation of your own functions

Some useful tips

Waffle

Chai matchers

expect(SOMETHING_TO_EXPECT_TO_HAPPEN).aMatcher(HAPPENED);

Advanced Waffle

WorldPurpose Contract

Test Coverage

describe('Test withdraw', () => {
it('You cant withdraw when your balance is empty', async () => {
const tx = worldPurpose.connect(addr1).withdraw();
await expect(tx).to.be.revertedWith("You don't have enough withdrawable balance");
});
});

Create tests for the setPurpose function

it("You can't override your own purpose", async () => {
await worldPurpose.connect(addr1).setPurpose("I'm the new world purpose!", {
value: ethers.utils.parseEther('0.10'),
});
const tx = worldPurpose.connect(addr1).setPurpose('I want to override the my own purpose!', {
value: ethers.utils.parseEther('0.11'),
});
await expect(tx).to.be.revertedWith('You cannot override your own purpose');
});
await worldPurpose.connect(addr1).setPurpose("I'm the new world purpose!", {
value: ethers.utils.parseEther('0.10'),
});
await expect(tx).to.be.revertedWith('You cannot override your own purpose');
});
it("set purpose success when there's no purpose", async () => {
const purposeTitle = 'Reduce the ETH fee cost in the next 3 months';
const purposeInvestment = ethers.utils.parseEther('0.1');
await worldPurpose.connect(addr1).setPurpose(purposeTitle, {
value: purposeInvestment,
});
// Check that the purpose has been set
const currentPurpose = await worldPurpose.getCurrentPurpose();
expect(currentPurpose.purpose).to.equal(purposeTitle);
expect(currentPurpose.owner).to.equal(addr1.address);
expect(currentPurpose.investment).to.equal(purposeInvestment);
// Check that the balance has been updated
const balance = await worldPurpose.connect(addr1).getBalance();
expect(balance).to.equal(purposeInvestment);
});
await expect(tx).to.emit(worldPurpose, 'PurposeChange').withArgs(addr1.address, purposeTitle, purposeInvestment);

Create tests for the withdraw function

it('Withdraw your previous investment', async () => {
const firstInvestment = ethers.utils.parseEther('0.10');
await worldPurpose.connect(addr1).setPurpose('First purpose', {
value: ethers.utils.parseEther('0.10'),
});
await worldPurpose.connect(addr2).setPurpose('Second purpose', {
value: ethers.utils.parseEther('0.11'),
});
const tx = await worldPurpose.connect(addr1).withdraw();// Check that my current balance on contract is 0
const balance = await worldPurpose.connect(addr1).getBalance();
expect(balance).to.equal(0);
// Check that I got back in my wallet the whole import
await expect(tx).to.changeEtherBalance(addr1, firstInvestment);
});

Run all tests together

Did you like this content? Follow me for more!

--

--

--

Full-stack engineer at @Soplaya with a lot of love for everything around technology https://ericci.dev

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

BrickEX ambition and where do we fit in the crypto start — up maze?

AMA Recap: MGG on NFTb Community

itemVerse P2E Background

Blockchain & The Quest for Women Empowerment

A LUNAtics dream, burning 0.5 LUNA in 21 days for free

Blockchain as a service: Newer possibilities for SMEs

ResDEX Demo + Community Q&A

ALGORAND UPGRADE OF BETANET

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
StErMi

StErMi

Full-stack engineer at @Soplaya with a lot of love for everything around technology https://ericci.dev

More from Medium

Deploy Solidity Contracts to an NFT Marketplace With Price Feed

Building a Full Stack NFT minting Dapp using Hardhat, ethers.js, Next.js, and TailwindCSS

An image of technologies used in this project

An unofficial audit of a Solidity Game smart contract: Wizards & Dragons Game

Simple Unit Testing for Solidity Smart Contracts — Pt. 2 — Debugging