Hello guys, here are the slides from the workshop on Blockchain Tests that I gave earlier this week, as well as some responses to the issues that were addressed during the session. I can say as my first presential workshop after pandemic, it was a great experience with a full room 😃
Unfortunately, there is no recording of it, but you may clone the repo and follow the coding instructions to build the test class and methods, then compare to version 2 of the project, which has the most recent version.
The require Solidity function guarantees validity of conditions that cannot be detected before execution. It checks inputs, contract state variables and return values from calls to external contracts.
The address type comes in two flavours, which are largely identical:
address: Holds a 20 byte value (size of an Ethereum address).
address payable: Same as address, but with the additional members transfer and send.
The idea behind this distinction is that address payable is an address you can send Ether to, while a plain address cannot be sent Ether.
Type conversions
Implicit conversions from address payable to address are allowed, whereas conversions from address to address payable must be explicit via payable(<address>).
Explicit conversions to and from address are allowed for uint160, integer literals, bytes20 and contract types.
Other tools that you can use to test Blockchain applications
Hello guys !! After my talk about Testing Blockchain Applications on the #TechKnowDay, I thought it was worth posting a guide on the same topic as I felt it was complex to also understand Blockchain. So, this is a quick tutorial on how to run tests with Truffle Framework.
If you are still wondering where you can use Blockchain, here it’s a table with the percentage of companies that are focusing on it and the use cases.
We are going to use a bit of Javascript for the web part and Solidity for the blockchain project. For this project you will need to have installed NPM and Node already. We are going to install Truffle as part of the setup. You will also need MetaMask and Ganache, all of them you can find bellow:
Open your MetaMask plugin and click on new Custom RPC Network. Type the following (Currency Symbol will be automatically populated after you type the Chain ID) and save.
Open Ganache, click on Quick Start (Ethereum). Double check if the server has this configuration clicking on the on top right and then Server tab.
Click on Show keys of the account you have created and copy the private key.
Back on MetaMask, click on Import Account and add the private key you have copied from the previous step.
Setting up the project
On your terminal run:
npm install -g truffle
If you want to follow the test steps only, then you need to download the first release that contains the installation files of the project already:
If you want to try the installation for yourself from the scratch, then just go to this link and follow the Getting Started guide. This guide won’t be focusing on the installation of the framework or the setup of the contracts and migrations.
Just a heads up that for this project I am using the pet-shop box instead of the MetaCoin from the Getting Started guide.
Installation
After downloading the project, you will need to run some commands to set it up. Open your terminal on the root of the project and run:
Run the development console
truffle develop
Compile and migrate the smart contracts. Inside the development console you don’t need to type the truffle command.
compile
migrate
To get out of the truffle console type
.exit
Create a new file inside of the test folder called TestAdoption.sol and import all the needed modules, such as assertions, new instances of deployed addresses and the contract that will be tested.
Add the testUserCanAdoptPet() function below the variables block.
Note that here you are creating a function that will test you can adopt a pet and for this you will need to get the address related to the adoption transaction and compare the returnedId with the expectedPetId address
Try to explore and add other asserts like checking if it’s not returning null.
function testUserCanAdoptPet() public {
uint256 returnedId = adoption.adopt(expectedPetId);
Assert.equal(
returnedId,
expectedPetId,
"Adoption of the expected pet should match what is returned."
);
}
Asserting the adopter of the pet
Add the testGetAdopterAddressByPetId() function below the previous function.
This function will check if the adopter address for that pet is the same from the adopters list
Try to explore and add other asserts like comparing the age of the pet is returning correctly, for that you would need to add the age on the Adoption.sol contract, then compile and migrate again.
function testGetAdopterAddressByPetId() public {
address adopter = adoption.adopters(expectedPetId);
Assert.equal(
adopter,
expectedAdopter,
"Owner of the expected pet should be this contract"
);
}
Asserting the list of adopters
Now add the testGetAdopterAddressByPetIdInArray() function below the previous function.
This function will check if the memory address for this petId is the same as the expectedAdopter
Almost the same test as before, but this time we are explicitly storing adopters in memory rather than contract’s storage and then comparing them.
function testGetAdopterAddressByPetIdInArray() public {
address[16] memory adopters = adoption.getAdopters();
Assert.equal(
adopters[expectedPetId],
expectedAdopter,
"Owner of the expected pet should be this contract"
);
}
}
You should have something like this:
pragma solidity >=0.5.0;
// The first two imports are referring to global Truffle files, not a `truffle` directory.
// Gives us various assertions to use in our tests.
import "truffle/Assert.sol";
// When running tests, Truffle will deploy a fresh instance of the contract being tested to the blockchain.
import "truffle/DeployedAddresses.sol";
// The smart contract we want to test.
import "../contracts/Adoption.sol";
contract TestAdoption {
// The address of the adoption contract to be tested
Adoption adoption = Adoption(DeployedAddresses.Adoption());
// The id of the pet that will be used for testing
uint256 expectedPetId = 8;
//The expected owner of adopted pet is this contract
address expectedAdopter = address(this);
// Testing the adopt() function
function testUserCanAdoptPet() public {
uint256 returnedId = adoption.adopt(expectedPetId);
Assert.equal(
returnedId,
expectedPetId,
"Adoption of the expected pet should match what is returned."
);
}
// Testing retrieval of a single pet's owner
function testGetAdopterAddressByPetId() public {
address adopter = adoption.adopters(expectedPetId);
Assert.equal(
adopter,
expectedAdopter,
"Owner of the expected pet should be this contract"
);
}
// Testing retrieval of pet owner storing getAdopters in memory
function testGetAdopterAddressByPetIdInArray() public {
// Store adopters in memory rather than contract's storage
address[16] memory adopters = adoption.getAdopters();
Assert.equal(
adopters[expectedPetId],
expectedAdopter,
"Owner of the expected pet should be this contract"
);
}
}
Running the tests
Open your terminal on the root of the project and run:
truffle test
If everything went okay you will see green checks on your terminal like this:
You can check the final code with the latest release (Spoiler alert: You will see pictures of my dog, my dog’s best friend, my friend’s cat and my previous dog)