Here is my value-testing program (all tests passed):
const People = artifacts.require("People");
const truffleAssert=require("truffle-assertions");
const BigNumber = require('bignumber.js');
contract("People", async function(accounts){
let instance;
beforeEach(async function(){
instance = await People.deployed()
});
it("should emit delete-person event with owner address", async function(){
await instance.createPerson("Babette", 16, 160, {from: accounts[4], value:
web3.utils.toWei("1", "ether")});
let tx = await instance.deletePerson(accounts[4]);
truffleAssert.eventEmitted(tx, 'personDeleted', {deletedBy: accounts[0]});
});
it("should adjust the balance properly in the contract", async function(){
let oldBalance = await instance.getBalanceContract();
await instance.createPerson("Babette", 16, 160, {from: accounts[5], value:
web3.utils.toWei("1", "ether")});
let newBalance = await instance.getBalanceContract();
assert(1*oldBalance + 1*web3.utils.toWei("1", "ether") === 1*newBalance, "balance not
properly adjusted in the contract");
})
it("should adjust the balance properly on the blockchain", async function(){
let oldBalance = await web3.eth.getBalance(instance.address);
await instance.createPerson("Babette", 16, 160, {from: accounts[6], value:
web3.utils.toWei("1", "ether")});
let newBalance = await web3.eth.getBalance(instance.address);
assert(1*oldBalance + 1*web3.utils.toWei("1", "ether") === 1*newBalance, "balance not
properly adjusted on the blockchain");
})
it("should reduce the balance in the contract to zero if all funds are withdrawn", async
function(){
await instance.withdrawAll();
let newBalance = await instance.getBalanceContract();
assert(1*newBalance === 0, "balance in the contract not reduced to zero");
})
it("should reduce the contract balance on the blockchain to zero if all funds are
withdrawn", async function(){
await instance.createPerson("Babette", 16, 160, {from: accounts[7], value:
web3.utils.toWei("1", "ether")});
await instance.withdrawAll();
let newBalance = await web3.eth.getBalance(instance.address);
assert(1*newBalance === 0, "contract balance on the blockchain not reduced to zero");
})
it("should not allow a non-owner to withdraw all funds", async function(){
await instance.createPerson("Babette", 16, 160, {from: accounts[8], value:
web3.utils.toWei("1", "ether")});
truffleAssert.fails(instance.withdrawAll({from: accounts[1]}));
})
it("should allow the owner to withdraw all funds", async function(){
await instance.createPerson("Babette", 16, 160, {from: accounts[3], value:
web3.utils.toWei("1", "ether")});
truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
})
it("should deposit all the withdrawn funds in the owner's account", async function(){
let oldAccountBalance = await web3.eth.getBalance(accounts[0]);
await instance.createPerson("Babette", 16, 160, {from: accounts[9], value:
web3.utils.toWei("1", "ether")});
let contractBalance = await instance.getBalanceContract();
let tx = await instance.withdrawAll();
let gasUsed = BigNumber(tx.receipt.gasUsed);
let gasPrice = BigNumber(20000000000);
let gasCost = gasPrice.times(gasUsed);
let newAccountBalance = await web3.eth.getBalance(accounts[0]);
assert(1*oldAccountBalance + 1*contractBalance - gasCost ===
1*newAccountBalance, "balance not properly deposited in the owner's account");
})
});