it("should be deleted only by owner", async () => {
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1",'ether')});
await truffleAssert.fails(instance.deletePerson(accounts[0],{from: accounts[1]}),truffleAssert.ErrorType.REVERT);
})
it("should actually be deleted by owner", async () => {
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1",'ether')});
await instance.deletePerson(accounts[0]);
let result= await instance.getPerson({from: accounts[0]});
assert(result.name === '', 'not deleted');
})
2nd assignment
it("contract balance is increased when a new person is added, and the increase is 1eth", async() => {
let instance = await People.deployed();
let initialBalance = await instance.balance();//web3.eth.getBalance(accounts[0]);
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1",'ether'), from: accounts[1]});
let currentBalance= await instance.balance();
assert(parseInt(currentBalance) === parseInt((parseInt(initialBalance)+parseInt(web3.utils.toWei("1",'ether')))),"it does not update contract balance when adding a new person");
});
it("owner can withdraw", async () => {
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1",'ether')});
await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}),"owner cant withdraw");
});
it("only the owner can withdraw", async () => {
let instance = await People.deployed();
await truffleAssert.fails(instance.withdrawAll({from: accounts[1]}));
});
it("balance iz zero after owner withdrew all coins", async () => {
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1",'ether')});
await instance.withdrawAll({from: accounts[0]});
let currentBalance= await instance.balance();
assert(parseInt(currentBalance) === 0,'balance not zero after owner withdrew all coins')
});
Hi @slavenR
I think you should build 2 tests for this one
Because the check you are doing on
balanceAddrChainBefore - balanceAddrChainAfter
is not correct.
balanceContractAfter - balanceContractBefore
will be one so you can test it with â==â
If you want to test the addressSender balance you should add the gas used for your transaction.
If i use 2 here your test pass, but it shouldnât
await instance.createPerson("Lisa",35,160, { from: addressSender, value: web3.utils.toWei("2", "ether") })
This works but the error handling is wrong for the second test. Should have used truffleAssert.passes. Oh well I know now. After going over the correct solution I realize I mistook contract owner with account owner. Thanks I learned a lot with this and the second one.
it("Do not allow an account that did not create the person to delete it:", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether"), from: accounts[0]});
await truffleAssert.fails(instance.deletePerson(accounts[0],{from: accounts[1]}),
truffleAssert.ErrorType.REVERT);
});
it("Only allow the account that created the person to delete it:", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether"), from: accounts[0]});
await instance.deletePerson(accounts[0],{from: accounts[0]});
let result = await instance.getPerson( {from: accounts[0]});
assert(result.name === "", "Person was not deleted!");
});
Hi @gabba , thx for comment. After seeing video from Filip I realized that I missed a point how to write tests for this assignement. For the part balanceContractAfter - balanceContractBefore == 1 ether here is code
it("should check if contract balance is increased by 1 ether", async function(){
const balanceContractBefore = parseFloat(await web3.eth.getBalance(instance.address))
await instance.createPerson("Lisa",35,160, { from: addressSender, value: web3.utils.toWei("1", "ether") })
const balanceContractAfter = parseFloat(await web3.eth.getBalance(instance.address))
assert((balanceContractAfter - balanceContractBefore) == web3.utils.toWei("1","ether"), "Contract balance not increased by 1 ether!");
})
but for this part that youâve said about calculating gas price, I didnât find solution. How to get gas used from createPerson transaction?
Testing to make sure only the owner can delete people:
it("should not allow non-owner to delete people", async function(){
let instance = await People.deployed();
await instance.createPerson("Maya", 25, 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("Maya", 25, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
});
Edit by @gabba: use the Preformatted text format to display code @Crypto_Jeff
@filip Iâm stuck calling âtruffle testâ. It seems like ganache cannot see the âpeopletest.jsâ file into the test folder.
I tried to reset the contract configuration in ganache (remove and re-add âtruffle-config.jsâ) and re-launched the âmigrate --resetâ in the terminal, but nothing happened.
Below you can find a screenshot of the error.
Thanks!!
Hi @slavenR
you can get the gas used during your transaction by inspecting the receipt
let tx = await instance.createPerson("Lisa",35,160, { from: addressSender, value: web3.utils.toWei("1", "ether") })
console.log(tx.receipt.gasUsed);
This value is the amount of gas used so you will have to multiply it by 20000000000, which is the default price per gas unit used by truffle.
I think the answers in this topics could help you
Value Assignment code:
it("should keep track of balance correctly", async function(){
let instance = await People.new();
await instance.createPerson("Jason", 23, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
let balance = await instance.balance();
let currentBalance = parseFloat(balance);
let blockchainBalance = await web3.eth.getBalance(instance.address);
assert(currentBalance == web3.utils.toWei("1", "ether") && currentBalance == blockchainBalance)
});
it("Owner can withdraw balance", async function(){
let instance = await People.new();
await instance.createPerson("Joe", 45, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
});
it("should not allow a non-owner to withdraw balance", async function(){
let instance = await People.new();
await instance.createPerson("Sam", 37, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.withdrawAll({from: accounts[2]}), truffleAssert.ErrorType.REVERT);
});
it("owner balance should increase after withdrawal", async function(){
let instance = await People.new();
await instance.createPerson("Steve", 29, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
let ownerCurrentBalance = parseFloat(await web3.eth.getBalance(accounts[0]));
await instance.withdrawAll();
let ownerBalanceAfter = parseFloat(await web3.eth.getBalance(accounts[0]));
assert(ownerCurrentBalance < ownerBalanceAfter, "Owners balance did not increase");
});
it("should reset balance to 0 after withdrawal", async function(){
let instance = await People.new();
await instance.createPerson("Lisa", 35, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await instance.withdrawAll();
let balance = await instance.balance();
let currentBalance = parseFloat(balance);
let blockchainBalance = await web3.eth.getBalance(instance.address);
assert(currentBalance == web3.utils.toWei("0", "ether") && currentBalance == blockchainBalance, "Balance was not 0 or did not match")
})
Thx @gabba for explanation. Here is test if sender address is really decreased by 1 ether.
it("should check if sender address is decresed by 1 ether", async function(){
const balanceSenderBefore = parseFloat(await web3.eth.getBalance(addressSender))
let tx = await instance.createPerson("Lisa",35,160, { from: addressSender, value: web3.utils.toWei("1", "ether") })
const balanceSenderAfter = parseFloat(await web3.eth.getBalance(addressSender))
const defaultGasTruffle = 20000000000;
const gasUsed = tx.receipt.gasUsed;
let balanceDiff = balanceSenderBefore - (balanceSenderAfter + (gasUsed*defaultGasTruffle))
assert(balanceDiff == web3.utils.toWei("1", "ether"), "Sender balance wasn't decreased by 1 ether!");
})
const People = artifacts.require('People');
const truffleAssert = require('truffle-assertions');
contract('People', async function (accounts) {
it('should not create a person with age over 150 years', async function () {
let instance = await People.deployed();
await truffleAssert.fails(
instance.createPerson('Bob', 200, 190, {
value: web3.utils.toWei('1', 'ether'),
}),
truffleAssert.ErrorType.REVERT
);
});
it('should not create a person without payment', async function () {
let instance = await People.deployed();
await truffleAssert.fails(
instance.createPerson('Bob', 20, 190, {
value: 1000,
}),
truffleAssert.ErrorType.REVERT
);
});
it('should set senior status correctly', async function () {
let instance = await People.deployed();
await instance.createPerson('Bob', 70, 190, {
value: web3.utils.toWei('1', 'ether'),
});
let result = await instance.getPerson();
assert(result.senior === true, 'Senior status not set');
});
it('should set age correctly', async function () {
let instance = await People.deployed();
let result = await instance.getPerson();
assert(result.age.toNumber() === 70, 'Age not set');
});
it('should not delete person without sending from contract owner address', async function () {
let instance = await People.deployed();
await truffleAssert.fails(
instance.deletePerson(accounts[0], { from: accounts[1] }),
truffleAssert.ErrorType.REVERT
);
});
it('should delete person sending from contract owner address', async function () {
let instance = await People.deployed();
await instance.deletePerson(accounts[0], { from: accounts[0] });
let person = await instance.getPerson();
assert(person.age.toNumber() === 0);
});
});
const People = artifacts.require('People');
const truffleAssert = require('truffle-assertions');
contract('People', async function (accounts) {
let instance;
beforeEach(async function () {
instance = await People.deployed();
});
it('should not 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('should not create a person without payment', async function () {
await truffleAssert.fails(
instance.createPerson('Bob', 70, 190, {
value: 1000,
}),
truffleAssert.ErrorType.REVERT
);
});
it('should set senior status correctly', async function () {
await instance.createPerson('Bob', 70, 190, {
value: web3.utils.toWei('1', 'ether'),
});
let result = await instance.getPerson();
assert(result.senior === true, 'Senior status not set');
});
it('should set age correctly', async function () {
let result = await instance.getPerson();
assert(result.age.toNumber() === 70, 'Age not set');
});
it('should not delete person without sending from contract owner address', async function () {
await instance.createPerson('John', 40, 185, {
from: accounts[5],
value: web3.utils.toWei('1', 'ether'),
});
await truffleAssert.fails(
instance.deletePerson(accounts[5], { from: accounts[5] }),
truffleAssert.ErrorType.REVERT
);
});
it('should delete person sending from contract owner address', async function () {
await truffleAssert.passes(
instance.deletePerson(accounts[5], { from: accounts[0] })
);
});
it('should have state and blockchain balances increase by the correct amount', async function () {
let instance = await People.new();
await instance.createPerson('Bob', 30, 190, {
from: accounts[0],
value: web3.utils.toWei('1', 'ether'),
});
let stateBalance = await instance.balance();
let blockchainBalance = await web3.eth.getBalance(instance.address);
assert(
web3.utils.fromWei(stateBalance.toString()) === '1' &&
web3.utils.fromWei(blockchainBalance) === '1'
);
});
it('should not withdraw from non owner address', async function () {
await truffleAssert.fails(
instance.withdrawAll({ from: accounts[5] }),
truffleAssert.ErrorType.REVERT
);
});
it('should withdraw from owner address', async function () {
await truffleAssert.passes(instance.withdrawAll({ from: accounts[0] }));
});
it('should reduce state and blockchain balances by proper amount when withdraw', async function () {
let instance = await People.new();
await instance.createPerson('Bob', 30, 190, {
from: accounts[5],
value: web3.utils.toWei('1', 'ether'),
});
await instance.withdrawAll({ from: accounts[0] });
let stateBalance = await instance.balance();
let blockchainBalance = await web3.eth.getBalance(instance.address);
assert(stateBalance.toString() === '0' && blockchainBalance === '0');
});
it('should credit the owners address with proper amount when withdrawing', async function () {
let instance = await People.new();
await instance.createPerson('Bob', 30, 190, {
from: accounts[6],
value: web3.utils.toWei('1', 'ether'),
});
let ownerBalance = await web3.eth.getBalance(accounts[0]);
await instance.withdrawAll();
let newBalance = await web3.eth.getBalance(accounts[0]);
assert(
parseFloat(ownerBalance) < parseFloat(newBalance),
'New balance has not increased after withdraw'
);
});
});
Hey all!
Here are my tests. Looking forward to many more challenges ahead
contract ("People", async function(accounts){
--
--
--
it("should delete a person as owner", async function(){
let instance = await People.deployed();
await instance.deletePerson(accounts[0]);
let result = await instance.getPerson();
assert(result.height.toNumber() === 0, "Person not deleted correctly");
})
it("shouldn't allow non-owners to delete a person", async function(){
let instance = await People.deployed();
await instance.createPerson("Waffles", 4, 20, {value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.deletePerson(accounts[0], {from: accounts[1]}), truffleAssert.ErrorType.REVERT);
});
});
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
contract ("People", async function(accounts){
let owner = accounts[0];
let nonOwner = accounts[1]
it("shouldnt create a person with age over 150 years", async function(){
let instance = await People.deployed();
await truffleAssert.fails(instance.createPerson("Bob", 200, 190, {value: web3.utils.toWei("1", "ether")}), truffleAssert.ErrorType.REVERT);
});
it("shouldnt create a person without payment", async function(){
let instance = await People.deployed();
await truffleAssert.fails(instance.createPerson("Bob", 50, 190, {value: 1000}), truffleAssert.ErrorType.REVERT);
});
it ("should set senior status correctly", async function(){ //promijenit tekst onda
let instance = await People.deployed();
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"); // moze se i sve zajedno "true && result.age.toNumber() === 65..."
});
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");
});
it("Only owner can delete accounts", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
await instance.deletePerson(owner);
let result = await instance.getPerson();
assert(result.age == 0, "Person deleted correctly");
});
it("should fail if not owner", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 64, 190, { value: web3.utils.toWei("1", "ether")});
await instance.deletePerson(nonOwner);
let result = await instance.getPerson();
assert(result.name.length > 0, "Person not deleted");
});
});
Value assigment
it("balance should be increased", async function(){
let initialBalance = await instance.balance();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1",'ether'), from: accounts[1]});
let currentBalance= await instance.balance();
assert(parseInt(currentBalance) === parseInt((parseInt(initialBalance)+parseInt(web3.utils.toWei("1",'ether')))),"it does not update contract balance when adding a new person");
});
it("owner can whitdraw balance", async function(){
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}), "owner cant withdraw");
});
it("only the owner can withdraw", async function() {
await truffleAssert.fails(instance.withdrawAll({from: accounts[1]}));
});
it("balance iz zero after whitdrawal", async function (){
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1",'ether')});
await instance.withdrawAll({from: accounts[0]});
let currentBalance= await instance.balance();
assert(parseInt(currentBalance) === 0,"balance in not zero after whitdrawal")
});
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("should allow the account owner to delete people", async ()=> {
await instance.createPerson("Carl", 24, 190, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
});
it("shouldn't allow another then the master account to delete people", async ()=> {
await instance.createPerson("Carl", 24, 190, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.deletePerson(accounts[2], {from: accounts[2]}), truffleAssert.ErrorType.REVERT);
});
});
Only Owner
//Only Owner modifier test
it("shouldn't delete a person unless called by contract owner", async function() {
let instance = await People.deployed();
await truffleAssert.fails(instance.deletePerson(accounts[0], {from: accounts[1]}), truffleAssert.ErrorType.REVERT);
});
it("should delete person because its called by contract owner", async function() {
let instance = await People.deployed();
await instance.deletePerson(accounts[0], {from: accounts[0]});
let result = await instance.getPerson();
assert(result.name === "", "Person has not been deleted");
})
I didnât create a new Person, I just used Bob that we created in previous tests.
Bob is made with account[0] so he shouldnât get deleted in the first test because account[1] is used.
Then he gets deleted in second test. I just watched the video, using truffleAssert.passes
is a lot better to use for second testâŚ
==================
Value Testing
async function load_balances(instance) {
let local = await instance.balance.call(function(err, res) {
return res;
});
let blockchain = await web3.eth.getBalance(instance.address)
return {local, blockchain}
};
//Value testing
it("should increase contract balance and match with local variable's balance", async function() {
let instance = await People.new();
await instance.createPerson("Lisa", 35, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")})
let balances = await load_balances(instance);
assert(web3.utils.fromWei(balances.blockchain, "ether") >= 1 && balances.blockchain == balances.local, "Eth has not been added to balance for createPerson");
});
it("shouldn't allow anyone but the owner to withdraw the balance", async function() {
await truffleAssert.fails(instance.withdrawAll({from: accounts[1]}), truffleAssert.ErrorType.REVERT);
});
it("should allow contract owner to withdraw the balance and balances should go to 0", async function() {
await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
let balances = await load_balances(instance);
assert(balances.local + balances.blockchain == 0, "Balances haven't changed to 0 after WithdrawAll")
});
Ok i completely forgot I can just do instance.balance to get the value of the uint public balance.
It creates getter function. And im relying too much on previous tests, i should be creating new contract and then creating new person to be sure everything is fine.
And I havenât checked if accounts[0] balance had increased. Everything else is fine i think.
Owner Test Assignment:
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
contract("People", async function(accounts){
it("shouldn't create a person with age over 150 years", async function(){
let instance = await People.deployed();
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(){
let instance = await People.deployed();
await truffleAssert.fails(instance.createPerson(
"Bob",100,190,{value: web3.utils.toWei("0.5", "ether")
}),
truffleAssert.ErrorType.REVERT);
});
it("should set senior status correctly", async function(){
let instance = await People.deployed();
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 instance = await People.deployed();
let result = await instance.getPerson();
assert(result.age.toNumber() === 65, "Age not set correctly");
});
it("should deleted only by the owner", async function(){
let instance = await People.deployed();
let owner = accounts[0];
await instance.deletePerson(owner, {from: owner});
let result = await instance.getPerson();
assert(result.age == 0, "Person hasn't been deleted");
});
it("shouldn't deletePerson from a normal user", async function(){
let instance = await People.deployed();
let owner = accounts[0];
let user = accounts[1];
await truffleAssert.fails(instance.deletePerson(owner, {from: user}), truffleAssert.ErrorType.REVERT);
});
});