Unit Test ERC20¶
To test the token, we want to change our usual setup a bit. Let's use chai's expect to test the transfer of tokens from the owner to another account.
Install Chai¶
First we need some additional npm packages:
npm install --save chai chai-bn chai-as-promised
Then create our test in the tests folder. Create a new file called /tests/MyToken.test.js:
const Token = artifacts.require("MyToken");
var chai = require("chai");
const BN = web3.utils.BN;
const chaiBN = require('chai-bn')(BN);
chai.use(chaiBN);
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const expect = chai.expect;
contract("Token Test", async accounts => {
const [ initialHolder, recipient, anotherAccount ] = accounts;
it("All tokens should be in my account", async () => {
let instance = await Token.deployed();
let totalSupply = await instance.totalSupply();
//old style:
//let balance = await instance.balanceOf.call(initialHolder);
//assert.equal(balance.valueOf(), 0, "Account 1 has a balance");
//condensed, easier readable style:
await expect(instance.balanceOf(initialHolder)).to.eventually.be.a.bignumber.equal(totalSupply);
});
});
Note
The next step would be testing, but the truffle version I was using has problems with the internal developer network from truffle. See this screenshot:
So, in order to make this work, I had to use Ganache + Truffle.
-
Open Ganache
-
Adjust the truffle-config.js file
-
Run the tests
Open Ganache GUI or Ganache-cli¶
If you want to strictly stay on the command line, then install ganache-cli (npm install -g ganache-cli) and run it from the command line. Mind the PORT number, which we need in the next step!
Otherwise roll with the GUI version of Ganache, which runs on Port 7545 usually (but double-check!)
Adjust the truffle-config.js file¶
If you are running Ganache-GUI then adjust the truffle-config.js file, so that the default development network is going to use the right host and port.
Also make sure the solc-version is the correct one and lock it in, if you haven't already:
const path = require("path");
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
contracts_build_directory: path.join(__dirname, "client/src/contracts"),
networks: {
development: {
port: 7545,
network_id: "*",
host: "127.0.0.1"
}
},
compilers: {
solc: {
version: "^0.6.0",
}
}
};
Test the Smart Contract¶
By going to your console, to the root folder of the project, and typing
in truffle test
, you will call the Trufle testing suite. If all goes
well it will give you this output:
Add more Tests to your Token-Test¶
Add some more tests to make sure everything works as expected.
// add this into the contract token test:
it("I can send tokens from Account 1 to Account 2", async () => {
const sendTokens = 1;
let instance = await Token.deployed();
let totalSupply = await instance.totalSupply();
await expect(instance.balanceOf(initialHolder)).to.eventually.be.a.bignumber.equal(totalSupply);
await expect(instance.transfer(recipient, sendTokens)).to.eventually.be.fulfilled;
await expect(instance.balanceOf(initialHolder)).to.eventually.be.a.bignumber.equal(totalSupply.sub(new BN(sendTokens)));
await expect(instance.balanceOf(recipient)).to.eventually.be.a.bignumber.equal(new BN(sendTokens));
});
it("It's not possible to send more tokens than account 1 has", async () => {
let instance = await Token.deployed();
let balanceOfAccount = await instance.balanceOf(initialHolder);
await expect(instance.transfer(recipient, new BN(balanceOfAccount+1))).to.eventually.be.rejected;
//check if the balance is still the same
await expect(instance.balanceOf(initialHolder)).to.eventually.be.a.bignumber.equal(balanceOfAccount);
});
//...
And run it again: