Hi @pedromndias
In your assert tests you are not testing the balance of the contract you are assigning a value.
Your =
should be ==
assert(contractBalance = await web3.eth.getBalance(instance.address), "balance should be the same");
Hi @pedromndias
In your assert tests you are not testing the balance of the contract you are assigning a value.
Your =
should be ==
assert(contractBalance = await web3.eth.getBalance(instance.address), "balance should be the same");
Hey @gabba, thanks for your reply. Good point, didn’t pay attention to that detail and now changing “=” to “==” it’s a failing festival lol:
3 passing (3s)
2 failing
Contract: People
should increase the balance:
AssertionError: balance should increase to 1
at Context.<anonymous> (test\peopletest.js:91:5)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
2) Contract: People
should be increased:
AssertionError: owners balance should be 1
at Context.<anonymous> (test\peopletest.js:111:5)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
My second and fifth test are claming some error on the assert function line, any thoughts?
Thanks once again!
Hi @pedromndias
Hum try to do your async call outside of your assert and store it into a variable.
Don’t hesitate to console.log the result before testing it if you need to debug it
Owner Test assignment :
const People=artifacts.require(“People”);
const truffleAssert=require(“truffle-assertions”);
contract(“People”,async function(accounts){
it("delete people function is availible for owner only! ", async function(){
let instance = await People.deployed();
await instance.createPerson(“Bob”, 40, 90, {from: accounts[2], value: web3.utils.toWei(“1”, “ether”)});
await truffleAssert.fails(instance.deletePerson(accounts[2], {from: accounts[2]}), truffleAssert.ErrorType.REVERT);
});
it(“Only owner can delete person”, async function(){
let instance = await People.new();
await instance.createPerson(“Bob”, 40, 90, {from: accounts[2], value: web3.utils.toWei(“1”, “ether”)});
await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
});
});
Value asignment
it(“Balance is increase after adding new Person”, async function(){
let instance = await People.deployed();
let Balance = await web3.eth.getBalance(People.address);
await instance.createPerson(“Lisa”, 28, 166, {value: web3.utils.toWei(“1”,“ether”), from: accounts[1]});
let newBalance = await web3.eth.getBalance(People.address);
assert(newBalance == parseInt(Balance) + parseInt(web3.utils.toWei(“1”,“ether”)));
});
it(“It is imposible widrow founds from contract if you are not owner”, async function(){
let instance = await People.deployed();
await truffleAssert.fails(instance.withdrawAll({from: accounts[1]}));
});
it("Conract owner is able widrow founds ", async function(){
let instance = await People.deployed();
await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
});
it("contract address balance", async function(){
let instance = await People.deployed();
let address = instance.address;
let currentBalance = await web3.eth.getBalance(address);
let currentbalanceInNumber = parseInt(currentBalance);
await instance.createPerson("Lisa", 35, 160, {from: accounts[3], value: web3.utils.toWei("1", "ether")});
currentbalanceInNumber += 1000000000000000000;
let finalBalance = await web3.eth.getBalance(address);
let finalBalanceInNumber = parseInt(finalBalance);
assert(currentbalanceInNumber === finalBalanceInNumber, "cost not transfer correctly");
});
it("should withdraw all", async function(){
let instance = await People.deployed();
let ownerBalance = await web3.eth.getBalance(accounts[0]);
let contractBalance = await web3.eth.getBalance(instance.address);
ownerBalance = parseInt(ownerBalance) + parseInt(contractBalance);
let txnReceipt = await instance.withdrawAll();
let gasUsed = txnReceipt.receipt.gasUsed;
gasUsed = parseInt(gasUsed)*20000000000;
ownerBalance -= gasUsed;
let address = instance.address;
let currentBalance = await web3.eth.getBalance(address);
let currentbalanceInNumber = parseInt(currentBalance);
let finalOwnerBalance = await web3.eth.getBalance(accounts[0]);
let finalBalanceInNumber = parseInt(finalOwnerBalance);
assert((currentbalanceInNumber === 0) && (ownerBalance === finalBalanceInNumber),"transfer not set correctly");
});type or paste code here
Very nice! You even combined them all… I like it.
Ivo
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(‘should not 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() {
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 level 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 correctly’);
});
it(‘should not allow deletion of person if not owner’, async function() {
let instance = await People.deployed();
let result = await instance.getCreator(0);
await instance.deletePerson(result);
assert.equal(result, accounts[0], ‘Incorrect person deleted’);
})
});
my solution is
... some other testcases ....
it("should only be possible for the owner to delete a person",
async function(){
let i = await People.deployed();
await i.createPerson("CBob", 65, 190,
{value:web3.utils.toWei("1","ether"), from: account[1]});
await truffleAssert.fails(
i.deletePerson(account[1], {from:account[2]})
, truffleAssert.ErrorType.REVERT);
});
it("should be possible to delete person by owner",
async function(){
let i = await People.deployed();
await i.deletePerson(account[0], {from:account[0]});
let person = await i.getPerson();
assert(person.name ==="" && person.age == 0 && person.height == 0 && person.senior == false);
});
....
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
contract("People", async function(accounts){
it("should allow the owner to delete the contract", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether"), from: accounts[0]});
await truffleAssert.passes(instance.deletePerson(accounts[0], {from: accounts[0]}), truffleAssert.ErrorType.REVERT);
});
it("should forbid a non-owner to delete the contract", 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);
});
});
Value assignment:
it("should increase balance variable when a person is added", async function(){
let instance = await People.new();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
let balance = await instance.balance();
assert(balance == web3.utils.toWei("1", "ether"), "balance not set correctly");
await instance.createPerson("Alice", 45, 170, {value: web3.utils.toWei("4", "ether")});
balance = await instance.balance();
assert(balance == web3.utils.toWei("5", "ether"), "balance not set correctly");
});
it("should increase balance on the blockchain when a person is added", async function(){
let instance = await People.new();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
let balance = await web3.eth.getBalance(instance.address);
assert(balance == web3.utils.toWei("1", "ether"), "balance not set correctly");
await instance.createPerson("Alice", 45, 170, {value: web3.utils.toWei("4", "ether")});
balance = await web3.eth.getBalance(instance.address);
assert(balance == web3.utils.toWei("5", "ether"), "balance not set correctly");
});
it("balance on the blockchain and balance variable should be the same", async function(){
let instance = await People.new();
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
let balance1 = await instance.balance();
let balance2 = await web3.eth.getBalance(instance.address);
assert(balance1 == balance2, "balance not set correctly");
await instance.createPerson("Alice", 45, 170, {value: web3.utils.toWei("4", "ether")});
balance1 = await instance.balance();
balance2 = await web3.eth.getBalance(instance.address);
assert(balance1 == balance2, "balance not set correctly");
});
following my solution. I’m not sure if it is common to use two assert() functions in one it() function
it("should increase balance when add a person and "+
"have the same balance in both smart contract and blockchain", async function(){
// get balance: web3.eth.getBalance(address);
let i = await People.deployed();
//get actual balance
let oldBalanceSC = parseFloat( await i.balance());
let balanceBC = parseFloat(await web3.eth.getBalance(i.address));
//compare balances in sc and bc
assert(oldBalanceSC == balanceBC, "first check Balances must be equal")
//add Person
await i.createPerson("DBob", 65, 190,
{value:web3.utils.toWei("1","ether")});
// compare with new balance
let newBalanceSC = parseFloat(await i.balance());
balanceBC = parseFloat(await web3.eth.getBalance(i.address));
let balanceSum = oldBalanceSC + parseFloat(await web3.utils.toWei("1","ether"));
//compare balances in sc and bc
assert(newBalanceSC == balanceBC && balanceSum == newBalanceSC
, "second check Balances must be equal")
});
it("should reduce balance to 0 when withdraw all funds and "+
"have the same balance in both smart contract and blockchain", async function(){
let i = await People.deployed();
//get actual balance
let balanceSC = parseFloat( await i.balance());
let balanceBC = parseFloat(await web3.eth.getBalance(i.address));
//compare balances in sc and bc
assert(balanceSC == balanceBC, "first check Balances must be equal")
//withdrawAll
await i.withdrawAll();
// compare with new balance
balanceSC = parseFloat(await i.balance());
balanceBC = parseFloat(await web3.eth.getBalance(i.address));
//compare balances in sc and bc
assert( web3.utils.toWei("0", "ether") == balanceSC && balanceBC == balanceSC
, "second check: Balances must be zero and equal ")
});
guys i need some help…tryed to reinstall the packages but nothing really change…
do u have any idea witf is going on??
sorry guys i need to upp this cause im still stuck…
Value Assignment ::
it(‘Contract balance should be incremented by 1 (one) after person is created’, async function() {
let instance = await People.deployed();
let before_balance = await instance.getBalance();
await instance.createPerson(‘Elijah’,50,150, {value: web3.utils.toWei(‘1’, ‘ether’)});
let current_balance = await instance.getBalance()
let _difference = current_balance -= before_balance;
assert.equal(_difference, 1000000000000000000, ‘Incorrect contract balance’);
});
it(‘Owner balance should equal Account balance on the blockchain’, async function() {
let instance = await People.deployed();
let owner = await instance.getOwner();
let owner_balance = await web3.eth.getBalance(owner);
let blockchain_balance = await web3.eth.getBalance(accounts[0]);
assert.equal(owner_balance, blockchain_balance, ‘Incorrect blockchain contraxct balance’);
});
it(‘Contract balance should be 0 (zero) after withdrawal’, async function () {
let instance = await People.deployed();
await instance.withdrawAll();
let contract_balance = await instance.getBalance();
assert.equal(contract_balance,0,‘Contract Is Not 0 (ZERO) after withdrawal’);
});
my solution to the onlyower modifier test.
it("should be deletable just from owner", async function(){
let instance = await People.deployed();
await instance.createPerson("Noob",33,180, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.deletePerson(accounts[1], {from: accounts[1]}) ,truffleAssert.ErrorType.REVERT);
})
it("should allow owner to delete People", async function(){
let instance = await People.deployed();
await instance.createPerson("Goob",35,181, {from: accounts[2],value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.deletePerson(accounts[2], {from: accounts[0]}) ,truffleAssert.ErrorType.REVERT);
})
Owner Test
it("should only contract owner (accounts[0]) delete", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await TruffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
});
it("shouldn't other user (accounts[1]) delete", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 65, 190, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await TruffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[1]}));
});