Hi.
You can use the Preformatted text
to show the code in the post.
Ivo
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]}));
});
You can use two assert if it make sense in your test, you just have to describe exactly what you are testing in the title of your test.
When you are testing two values use the ===
instead of ==
because the double equal is performing a type conversion and it can leads to error later in your code:
ex:
"42" == 42
true
"42" === 42
false
Hello @Gab
in your last test you are not declaring the account which create a person, so by default the accounts[0] is chosen.
So the value before and after the withdraw will be equal.
You can add console.log() in your code to help you during the debug
@filip and everyone
Why do we have access to web3 in our test.js files?
Does truffle automatically import it for us?
Thanks in advance
Owner test Assignment
it("Should not delete if you are not the owner", async function () {
let instance = await People.deployed();
await truffleAssert.passes(
instance.createPerson("Bob", 132, 190, {
value: web3.utils.toWei("1", "ether"),
from: accounts[0],
})
);
await truffleAssert.fails(
instance.deletePerson(
accounts[0],
{ from: accounts[1] },
truffleAssert.ErrorType.REVERT
)
);
});
I noticed such statement
let abalance = await web3.utils.toBN(web3.eth.getBalance(instance.address));
gives me an error:
Error: Error: [number-to-bn] while converting number {âdomainâ:{âdomainâ:null,"_events":{},"_eventsCount":1,âmembersâ:[]}} to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: â[object Promise]â
Why is it so and how to overcome? getBalance returns string AFAIK and I would like to convert it from wei to ether and then add some value
Value Assignment
it("Should increase balance to 1", async function () {
let instance = await People.new();
await instance.createPerson("Bob", 132, 190, {
value: web3.utils.toWei("1", "ether"),
from: accounts[0],
});
const instanceBalance = web3.utils.fromWei(await instance.balance(), 'ether');
const contractAddressBalance = web3.utils.fromWei(await web3.eth.getBalance(instance.address));
await assert(instanceBalance === '1');
await assert(instanceBalance === contractAddressBalance);
});
it("Should set balance to 0", async function () {
let instance = await People.new();
await instance.createPerson("Bob", 132, 190, {
value: web3.utils.toWei("1", "ether"),
from: accounts[0],
});
await instance.withdrawAll();
const instanceBalance = web3.utils.fromWei(await instance.balance(), 'ether');
const contractAddressBalance = web3.utils.fromWei(await web3.eth.getBalance(instance.address));
await assert(instanceBalance === '0');
await assert(instanceBalance === contractAddressBalance);
});
Value assignment
contract("People", async function(accounts){
let instance;
before(async function(){
instance = await People.deployed();
});
it("contract balance after createPerson should be greater than value sent", async function(){
console.log("deployed address:" + instance.address);
let balance_before = await web3.eth.getBalance(instance.address);
var b = parseFloat(balance_before);
console.log("contract balance:" + balance_before);
await instance.createPerson("Bob", 65, 190, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
let balance_after = await web3.eth.getBalance(instance.address);
var a = web3.utils.fromWei(balance_after, "ether").toString();
console.log("contract balance:" + a);
assert.equal(b + 1, a, "Contract balance not updated corectly");
});
it("contract balance after withdrawal should equal to 0 (balance variable)", async function(){
await instance.withdrawAll();
var bb = await instance.balance();
console.log("actual balance: " + bb);
assert(bb == 0, "balance variable not equals 0");
assert(await web3.eth.getBalance(instance.address) == bb, "contract ballance not equal to variable balance" )
});
});
Here is my code for the deletePerson function:
it("should not allow non-owner to delete a person.", async function(){
await instance.createPerson("Lisa", 35, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.reverts(instance.deletePerson(accounts[2], {from: accounts[2]}));
});
it("should allow the owner to delete a person.", async function(){
let instance = await People.new();
await instance.createPerson("Lisa", 30, 180, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.deletePerson(accounts[1]), {from: accounts[0]});
});
Value Assignment:
it("should allow the owner to withdraw balance", async function(){
let instance = await People.new();
await instance.createPerson("Bob", 65, 190, {from: accounts[1], 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("Bob", 65, 190, {from: accounts[0], value: web3.utils.toWei("1", "ether")});
await truffleAssert.reverts(instance.withdrawAll({from: accounts[1]}));
});
it("owners balance should increase after withdrawal", async function(){
let instance = await People.new();
await instance.createPerson("Lisa", 35, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
let balanceStart = parseFloat(await web3.eth.getBalance(accounts[0]));
await instance.withdrawAll({from: accounts[0]});
let balanceEnd = parseFloat(await web3.eth.getBalance(accounts[0]));
assert(balanceStart < balanceEnd, "Owners balance was not increased after withdrawal");
});
it("contract balance should reset to 0 after withdrawal", async function(){
let instance = await People.new();
await instance.createPerson("Lisa", 35, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await instance.withdrawAll();
let contractBalance = parseFloat(await instance.balance);
let ethBalance = await web3.eth.getBalance(instance.address);
assert(contractBalance == 0, "Contract balance was not 0 after withdrawal");
assert(ethBalance == 0, "Balance on Blockchain was not 0 after withdrawal");
assert(contractBalance == ethBalance, "Contract balance and balance on Blockchain were not the same.");
});
TEST for owner and deleting account
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
const Ownable = artifacts.require("Ownable");
contract("People", async function(accounts){
let instance;
before(async function(){
instance = await People.deployed()
});
it("shouldn't 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("shouldn't create a person without payment", async function(){
await truffleAssert.fails(instance.createPerson("Bob", 50, 190, {value:1000}), truffleAssert.ErrorType.REVERT);
});
it("should set senior status correctly", async function(){
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 result = await instance.getPerson();
assert(result.age.toNumber() === 65, "Age not set correctly");
});
contract("Ownable", accounts => {
const [deleteAccount] = accounts;
it("sets an owner" , async () => {
const owner = await Ownable.new();
assert(await instance.getPerson (), deleteAccount);
});
});
})
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions")
contract("People", async function(accounts){
let owner = accounts[0];
let nonOwner = accounts[1]
it("shouldn't create a persong 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 a 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", 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 delete only creator's person", async function(){
let instance = await People.deployed();
await instance.createPerson("Bob", 64, 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 creator's person", 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");
})
})
Test Value Assignment
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions")
contract("People", async function(accounts){
let addressOwner = accounts[0];
let addressSender = accounts[1]
let instance;
before(async function(){
instance = await People.deployed()
})
it("shouldn't create a persong 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("shouldn't create a person without a payment", async function(){
await truffleAssert.fails(instance.createPerson("Bob", 50, 190, { value: 1000}), truffleAssert.ErrorType.REVERT);
})
it("should set senior status correctly", async function(){
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 result = await instance.getPerson();
assert(result.age.toNumber() === 65, "Age not set correctly");
})
it("should not allow non-owner to delete people", async function(){
await instance.createPerson("Lisa",35,160, { 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 the owner to delete people", async function(){
await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
})
// check the balance of contract and check balance of a contract address on blockchain when the user is added
//check if then contract owner can withdraw that balance,
//and check if balance is reduced to 0 and if that matches to blockchain address to whom we withdraw it(that balance has to increase)
it("should check if contract balance is increased by 1 eather, and blockchain balance of address decresed by 1 ether", async function(){
const balanceContractBefore = await web3.eth.getBalance(instance.address)
const balanceAddrChainBefore = await web3.eth.getBalance(addressSender)
await instance.createPerson("Lisa",35,160, { from: addressSender, value: web3.utils.toWei("1", "ether") })
const balanceContractAfter = await web3.eth.getBalance(instance.address)
const balanceAddrChainAfter = await web3.eth.getBalance(addressSender)
assert((balanceContractAfter - balanceContractBefore >= 1000000000000000000) && (balanceAddrChainBefore - balanceAddrChainAfter >= 1000000000000000000), "Balance check not passed!");
})
it("should check if contract balance is 0 after witdrawal, and owner balance is increased by withdrawn balance", async function(){
const balanceContract = await web3.eth.getBalance(instance.address)
const balanceOwnerBefore = parseFloat(await web3.eth.getBalance(accounts[0]))
await instance.withdrawAll()
const balanceContractAfter = await web3.eth.getBalance(instance.address)
const balanceOwnerAfter = parseFloat(await web3.eth.getBalance(accounts[0]))
assert(balanceContractAfter == 0 && balanceOwnerAfter > balanceOwnerBefore , "Balance withdraw check not passed!")
})
})