My solution to the last unit testing task:
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
contract("People", async function(accounts){
let instance;
before(async function(){
instance = await People.deployed()
});
it("shouldn't create a person with age over 150 years", async function(){
await truffleAssert.fails(instance.createPerson("Bob", 200, 190, {value: web3.utils.toWei("1", "ether")}), truffleAssert.ErrorType.REVERT);
});
it("shouldn't create a person without payment", async function(){
await truffleAssert.fails(instance.createPerson("Bob", 50, 190, {value: 1000}), truffleAssert.ErrorType.REVERT);
});
it("should set senior status correctly", async function(){
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
let result = await instance.getPerson();
assert(result.senior === true, "Senior level not set");
});
it("should set age correctly", async function(){
let result = await instance.getPerson();
assert(result.age.toNumber() === 65, "Age not set correctly");
});
it("should not allow non-owner to delete people", async function(){
let instance = await People.deployed();
await instance.createPerson("Lisa", 35, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.deletePerson(accounts[2], {from: accounts[2]}), truffleAssert.ErrorType.REVERT);
});
it("should allow the owner to delete people", async function(){
let instance = await People.new();
await instance.createPerson("Lisa", 35, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
});
it("should increase and correctly update balance when person is added and it matches to the balance on the blockchain", async function(){
let instance = await People.new();
let balanceAfterAdd = 1;
await instance.createPerson("Lisa", 35, 160, {from: accounts[3], value: web3.utils.toWei("1", "ether")})
await truffleAssert.passes(instance.balance === balanceAfterAdd && instance.balance === web3.eth.getBalance(0xc6a2c1ef58b4745729ef051173409ac40c3363c4));
});
it("should let the owner withdraw the balance of the contract", async function(){
let instance = await People.new();
await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
});
it("shouldnt allow non owner to withdraw the balance of the contract", async function(){
let instance = await People.deployed();
await instance.createPerson("Lisa", 35, 160, {from: accounts[4], value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.withdrawAll({from: accounts[4]}), truffleAssert.ErrorType.REVERT);
});
it("should reset the balance to 0 after the withdrawal and should match to the balance of the blockchain", async function(){
let instance = await People.new();
let balanceAfterReset = 0;
await instance.createPerson("Lisa", 35, 160, {from: accounts[5], value: web3.utils.toWei("1", "ether")});
await instance.createPerson("Lisa", 36, 160, {from: accounts[6], value: web3.utils.toWei("1", "ether")});
await instance.withdrawAll({from: accounts[0]});
await truffleAssert.passes(instance.balance === balanceAfterReset && instance.balance === web3.eth.getBalance(0xc6a2c1ef58b4745729ef051173409ac40c3363c4));
});
it("should update the balance of the owner", async function(){
let instance = await People.new();
await instance.createPerson("Lisa", 35, 160, {from: accounts[7], value: web3.utils.toWei("1", "ether")});
let balanceBefore = web3.eth.getBalance(accounts[0]);
await instance.withdrawAll({from: accounts[0]});
let balanceAfter = web3.eth.getBalance(accounts[0]);
await truffleAssert.passes(balanceAfter > balanceBefore);
});
});
//1. check when person is added, the balance of the contract gets increased AND it matches to what is registered to the balance that is registered to the contract address on the blockchain
//2. the contract owner can withdraw the ballance AND ballance is reduced to 0 and it matches to the blockchain AND owners balance is increased
//web3.eth.getBalance(address) -> query eth blockchain for the balance of a specific address (needed for comparing balance variable and real contract balance)
//need to find contract address in truffle