The first deletePerson call will not work correctly and therefore pass the truffleAssert.fails test:
it("should not allow others to delete people", async function(){
let instance = await People.deployed();
let creator = await instance.getCreator(0);
await truffleAssert.fails(instance.deletePerson(creator, {from: accounts[1]}),
truffleAssert.ErrorType.REVERT);
});
This second call will not pass the truffleAssert.fails (meaning the deletePerson function will work correctly):
it("should allow the owner to delete people", async function(){
let instance = await People.deployed();
let creator = await instance.getCreator(0);
await truffleAssert.passes(instance.deletePerson(creator, {from: accounts[0]}),
truffleAssert.ErrorType.REVERT);
});
(I decided to use get the account info from the contract assuming I donât know who made it, but also assuming I know the index of the person I want to remove.)
This was a good challenge, really made me think.
UPDATEâ> after seeing @filipâs answer I then changed the second truffleAssert.fails to truffleAssert.passes
Nice one Filip
Here are the tests for the last assignment:
it("should have a balance increase after createPerson", async function(){
instance = await People.new();
let onChainBalanceBefore = await web3.eth.getBalance(instance.address);
await instance.createPerson("Lisa", 65, 177, {value: web3.utils.toWei("1", "ether"), from: accounts[3]});
let onChainBalanceAfter = await web3.eth.getBalance(instance.address);
let balanceVarAfter = await instance.contractBalance();
assert(balanceVarAfter == onChainBalanceAfter && onChainBalanceAfter > onChainBalanceBefore, "The balance did not increase correctly.");
});
it("should have a zero balance after increasing ownerBalance", async function(){
let ownerBalance = await web3.eth.getBalance(accounts[0]);
await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}),"The withdrawAll function not called by owner");
let onChainBalance = await web3.eth.getBalance(instance.address);
let newOwnerBalance = await web3.eth.getBalance(accounts[0]);
let variableBalance = await instance.contractBalance();
assert(variableBalance == 0 && onChainBalance == 0 && newOwnerBalance > ownerBalance, "The balance wasn't withdrawn.");
});