My truffleAssert.fails() tests are working fine, but I’m having a bit of an issue with the assert tests testing the senior boolean and the age. It also edited the personCreated event in order to check the both the age and senior values and it says both are correct there, so I’m not sure why my assert functions fail saying senior is not set to true and age is not set correctly?
For the last test, when I use toNumber() like in the video I get a TypeError, but when I remove the toNumber() the assert simply fails. Why is this?
Is there something wrong with the getPerson() function such that result is not actually storing the created person into result?
.sol code
pragma solidity 0.5.12;
//pragma experimental ABIEncoderV2;
import "./Ownable.sol";
contract People is Ownable {
struct Person {
string name;
uint age;
uint height;
bool senior;
}
event personCreated(string name, uint age, uint height, bool senior);
event personDeleted(string name, bool senior, address deletedBy);
uint public balance;
modifier costs(uint cost) {
require(msg.value >= cost);
_;
}
mapping (address => Person) private people;
address[] private creators;
//costs 1 ether to run
function createPerson(string memory _name, uint _age, uint _height) public payable costs(1 ether) {
require(_age < 150, "TOO OLD!");
//require(msg.value >= 1 ether); //requires 1 wei to execute
//balance += msg.value; //add to balance value that was sent to this function/contract
Person memory newPerson;
newPerson.name = _name;
newPerson.age = _age;
newPerson.height = _height;
if(_age >= 65) {
newPerson.senior = true;
} else {
newPerson.senior = false;
}
insertPerson(newPerson);
creators.push(msg.sender);
//assert using to == compare hash of both using keccak256 hash function
//essentially checking if people[msg.sender] == newPerson
assert(
keccak256(
abi.encodePacked(
people[msg.sender].name,
people[msg.sender].age,
people[msg.sender].height,
people[msg.sender].senior
)
)
==
keccak256(
abi.encodePacked(
newPerson.name,
newPerson.age,
newPerson.height,
newPerson.senior
)
)
);
emit personCreated(newPerson.name, newPerson.age, newPerson.height, newPerson.senior);
}
function insertPerson(Person memory _person) private {
address creator = msg.sender;
people[creator] = _person;
}
/*for use with ABIEncoderV2
function getPerson() public view returns (Person memory) {
return people[msg.sender];
}*/
function getPerson() public view returns (string memory, uint, uint, bool) {
address creator = msg.sender;
return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
}
function deletePerson(address creator) public onlyOwner {
string memory name = people[creator].name;
bool senior = people[creator].senior;
delete people[creator];
assert(people[creator].age == 0);
emit personDeleted(name, senior, msg.sender);
}
function getCreator(uint index) public view onlyOwner returns (address) {
return creators[index];
}
function withdrawAll() public onlyOwner returns (uint) {
uint toTransfer = balance; //make temp var to send, reset balance to 0, done for security reasons
balance = 0;
msg.sender.transfer(toTransfer); //transfer can only be used by an address, transfers to address, which in this case
//is the owner, or msg.sender if already here. Reverts if transfer fails
/*
msg.sender.send(toTransfer); //this is the same as transfer but returns false if fails instead of reverts.
//hence, this requires manual reverting or else funds are lost.
if(msg.sender.send(toTransfer)) {
return toTransfer;
} else {
balance = toTransfer;
return 0;
}
*/
return toTransfer;
}
}
test.js code
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
contract("People", async function(){
//test and assert contract shouldn't make person with age over 150
it("shouldn't make person with age over 150", async function() {
let instance = await People.deployed();
//use assert to test failure, asserting that createPerson fails if age 200 since it's >150
await truffleAssert.fails(
instance.createPerson("Bob", 200, 300, {value: web3.utils.toWei("1", "ether")}),
truffleAssert.ErrorType.REVERT //test passes upon REVERT, we WANT to assert contract REVERT if age >150
);
})
//test and assert contract shouldn't create person without valid payment amount
it("shouldn't create a person without payment", async function(){
let instance = await People.deployed();
await truffleAssert.fails(
instance.createPerson("Bob", 50, 300, {value: 1000}),
truffleAssert.ErrorType.REVERT //test passes upon REVERT, we WANT to assert contract REVERT if age >150
);
})
//test and assert contract should set senior flag correctly
it("should set senior status correctly", async function() {
//create person with age >65
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 300, {value: web3.utils.toWei("1", "ether")});
let result = await instance.getPerson();
assert(result.senior === true, "should be senior!");
})
it("should set age correctly", async function() {
let instance = await People.deployed();
let result = await instance.getPerson();
assert(result.age.toNumber() === 65, "Age not set correctly!");
})
});
Terminal output:
Thanks for all the help!