Unit Testing Smart Contracts

).
To set the stage: what is unit testing and why do you need it for your smart contract? Well, let me put it this way imagine you have a fancy new car that you just spent all your savings on. You’re driving down the road, feeling pretty good about yourself, when suddenly… BOOM! Your engine explodes in flames (not ideal).
Now, if you had taken the time to test each individual part of your car before hitting the open road, chances are that little mishap wouldn’t have happened. Same goes for smart contracts by testing each function and edge case separately, you can catch any potential issues early on and avoid a catastrophic failure (or worse yet, losing all your money).
So how do you go about unit testing your smart contract? Well, first things first: write some code! Here’s an example of what that might look like in Solidity:
pragma solidity ^0.8.1;
contract MyContract {
uint public myVariable = 5; // this is a variable that we can access from outside the contract

function setMyVariable(uint _newValue) external { // this is a function that allows us to change the value of our variable
myVariable = _newValue;
}
}
Now, let’s say you want to test whether your `setMyVariable` function actually works. Here’s what that might look like in JavaScript (using Truffle):
const MyContract = artifacts.require(“./MyContract”); // import our contract from the file system
contract(‘MyContract’, (accounts) => { // create a new test suite called “MyContract” and pass it an array of accounts to use for testing purposes
let myContract; // declare a variable called `myContract` that will hold our instance of the MyContract contract

before(async () => { // run this code once, before any other tests are executed
myContract = await MyContract.deployed(); // deploy our contract to the test network and store it in the `myContract` variable
});

describe(‘setMyVariable’, () => { // create a new section called “setMyVariable” that will contain all of our tests related to this function
it(“should set myVariable to a new value”, async () => { // run this test and print out the result (if any)
const oldValue = await myContract.myVariable(); // get the current value of `myVariable` using the `myVariable()` method from our contract instance

// set a new value for `myVariable` using the `setMyVariable(uint _newValue)` function and passing in a new value (in this case, 10)
await myContract.setMyVariable(10);

const newValue = await myContract.myVariable(); // get the current value of `myVariable` again using the `myVariable()` method from our contract instance

assert.equal(newValue, 10, “myVariable should have been set to a new value”); // check that the new value is equal to what we expected (in this case, 10)
});
});
});
By following these simple steps and writing some code, you can catch any potential issues early on and avoid a catastrophic failure. And who knows? Maybe one day your fancy new car won’t explode in flames (or at least not as often).

SICORPS