Unit Testing in Truffle

I wasn’t able to get this one on my own unfortunately. I kept trying to plug accounts into the functions in the tests instead of at the top. That resulted in accounts not being defined. After watching the solution, I was easily able to see my mistake.

onlyOwner solution below.

const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");

contract("People", async function(accounts){
  it("shouldn't create a person over 150 age", async function() {
    let instance = await People.deployed();
    await truffleAssert.fails(instance.createPerson("Bob", 200, 200, {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", 50, 200, {value: 1000}), truffleAssert.ErrorType.REVERT);
  });
  it("should set senior status correctly", async function(){
    let instance = await People.deployed()
    await instance.createPerson("Bob", 65, 200, {value: web3.utils.toWei("1", "ether")}), truffleAssert.ErrorType.REVERT;
    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 confirm sender is owner", async function(){
    let instance = await People.deployed();
    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 owner to delete people", async function(){
    let instance = await People.deployed();
    await truffleAssert.passes(instance.deletePerson(accounts[1]), {from: accounts[0]})
  });
});```
1 Like

This passes the test, but it feels like a workaround and not the correct way to do it. Watching the solution video next.

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("shouldn't create a person over 150 age", async function() {
    await truffleAssert.fails(instance.createPerson("Bob", 200, 200, {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, 200, {value: 1000}), truffleAssert.ErrorType.REVERT);
  });
  it("should set senior status correctly", async function(){
    await instance.createPerson("Bob", 65, 200, {value: web3.utils.toWei("1", "ether")}), truffleAssert.ErrorType.REVERT;
    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 confirm sender is owner", 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 owner to delete people", async function(){
    let instance = await People.new();
    await instance.createPerson("Lisa", 35, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
    await truffleAssert.passes(instance.deletePerson(accounts[1]), {from: accounts[0]});
  });
  it("should confirm amount transferred to contract", async function(){
    instance.balance = 0;
    await instance.createPerson("Lisa", 35, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
    instance.balance += 1000000000000000000;
    assert(instance.balance === 1000000000000000000, "Balance not correct");
  });
  it("should allow owner to withdraw", async function(){
    let ownerPrevBal = accounts[0].balance;
    let amt = await instance.withdrawAll();
    instance.balance = 0;
    let ownerNewBal = ownerPrevBal + amt;
    assert({from: accounts[0]}, "Owner not calling function");
    assert(ownerNewBal === ownerPrevBal + amt, "Owner balance not updated");
    assert(instance.balance === 0, "Contract balance not updated");
  });
});

Value test assignment:

const People = artifacts.require("People");

const truffleAssert = require("truffle-assertions");

contract("People", async function(accounts){

  let instance;

  before(async function(){
    instance = await People.deployed();
  });

  // before runs code once before all tests can also use beforeEach if you want a fresh instance for each test
  // there is also after and afterEach for logic that you want to run after functions.

  it("shouldn't create a person with age over 150 years", async function(){
    //let instance = await People.deployed(); Moved to before
    await truffleAssert.fails(instance.createPerson("Bob", 200, 190, {value: web3.utils.toWei("1", "ether")}), truffleAssert.ErrorType.REVERT);
  });

  it("shouldn't allow create a person without payment", async function(){
    await truffleAssert.fails(instance.createPerson("John", 50, 190, {value: 1000}), truffleAssert.ErrorType.REVERT);
  });

  it("should set seniour status correctly", async function(){
    await instance.createPerson("Paul", 65, 190, {value: web3.utils.toWei("1", "ether")});
    let result = await instance.getPerson();
    assert(result.senior === true, "Senior level not set");
  });

  it("shouldn't allow non-owner to delete person", async function(){
    instance = await People.new();
    await instance.createPerson("Sue", 65, 190, {value: web3.utils.toWei("1", "ether"), from: accounts[0]});
    await truffleAssert.fails(instance.deletePerson(accounts[0],{value:0, from: accounts[1]}), truffleAssert.ErrorType.REVERT);
  });

  it("should allow owner to delete person", async function(){
    await instance.createPerson("Alice", 65, 190, {value: web3.utils.toWei("1", "ether"), from: accounts[1]});
    await truffleAssert.passes(instance.deletePerson(accounts[1],{from: accounts[0]}));
    let result = await instance.getPerson({from: accounts[1]});
    assert(result.name !== "Alice", "Person wasn't deleted");
  });

  it("shouldn't allow non-owner to withdrawAll", async function(){
    await truffleAssert.fails(instance.withdrawAll({from: accounts[1]}), truffleAssert.ErrorType.REVERT);
  });

  it("should allow owner to withdrawAll", async function(){
    await instance.createPerson("Alice", 65, 190, {value: web3.utils.toWei("1", "ether"), from: accounts[1]});
    await instance.withdrawAll({from: accounts[0]});
    let newbalance = await web3.eth.getBalance(instance.address);
    assert(newbalance == 0, "Balance isn't zero after withdrawAll but is: " + newbalance);
  });

  it("should have proper balances after adding person", async function(){
    instanceBalanceBefore = await instance.balance();
    contractBalanceBefore = await web3.eth.getBalance(instance.address);
    assert (instanceBalanceBefore == contractBalanceBefore, "instance and contract balance differ before : " + instanceBalanceBefore + " - " + contractBalanceBefore);
    await instance.createPerson("Bob", 50, 190, {value: web3.utils.toWei("1", "ether")});
    instanceBalanceAfter = await instance.balance();
    contractBalanceAfter = await web3.eth.getBalance(instance.address);
    assert (instanceBalanceAfter == contractBalanceAfter, "instance and contract balance differ after");
    assert ((instanceBalanceAfter - instanceBalanceBefore) == (web3.utils.toWei("1", "ether")), "balance didn't increase with 1 ether after adding person");

  });


});

1 Like

both assignments

contract("People", async function(accounts){
    let instance;
    beforeEach(async function(){
        instance = await People.new();
    });
    it("shouldn't delete person when I'm not the owner ", async function(){
        await instance.createPerson("Bob", 80, 190, {value: web3.utils.toWei("1","ether"), from: accounts[2]});
        await truffleAssert.fails(instance.deletePerson(accounts[2], {from: accounts[1]}), truffleAssert.ErrorType.REVERT);
    });
    it("should allow delete person when I'm the owner ", async function(){
        await instance.createPerson("Bob", 80, 190, {value: web3.utils.toWei("1","ether"), from: accounts[1]});
        await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
    });
    it("should let onwer allow withdraw balance", async function(){
        await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
    });
    it("shouldn't let nonowner allow withdraw balance", async function(){
        await truffleAssert.fails(instance.withdrawAll({from: accounts[1]}),truffleAssert.ErrorType.REVERT);
    });
    it("owner balance should increase after withdrawal", async function(){
        await instance.createPerson("Bob", 80, 190, {value: web3.utils.toWei("1","ether"), from: accounts[1]});
        let ownerOldBalance =  parseFloat(await web3.eth.getBalance(accounts[0]));
        await instance.withdrawAll();
        let ownerNewBalance =  parseFloat(await web3.eth.getBalance(accounts[0]));
        assert(ownerNewBalance > ownerOldBalance, "balance didn't increase")
    });
    it("should reset balance to 0 after withdrawal", async function(){
        await instance.createPerson("Bob", 80, 190, {value: web3.utils.toWei("1","ether"), from: accounts[1]});
        await instance.withdrawAll();
        let balance = parseFloat(await instance.balance());
        let networkBalance = parseFloat(await web3.eth.getBalance(instance.address));
        assert(balance == networkBalance, "network and local balance doesn't match");
        assert(balance == 0, "local balance isn't 0");
        assert(networkBalance == 0, "network balance isn't 0");

    });
    it("should increase balance correctly when adding a person", async function() {
        let oldBalance = parseFloat(await instance.balance());
        await instance.createPerson("Bob", 80, 190, {value: web3.utils.toWei("1","ether"), from: accounts[1]});
        let newBalance = parseFloat(await instance.balance());
        let networkBalance = parseFloat(await web3.eth.getBalance(instance.address));
        assert(newBalance == oldBalance + web3.utils.toWei("1","ether"), "Balance didn't increased by 1");
        assert(newBalance == networkBalance, "local and network balance didn't update correctly");
    });
});
1 Like

I am getting this when I try to compile:

PS C:\Users\alanm> cd Documents
PS C:\Users\alanm\Documents> cd code
PS C:\Users\alanm\Documents\code> cd People
PS C:\Users\alanm\Documents\code\People> truffle console
truffle(ganache)> migrate

Compiling your contracts…

Compiling .\contracts\Ownable.sol
Artifacts written to C:\Users\alanm\Documents\code\People\build\contracts
Compiled successfully using:

  • solc: 0.5.12+commit.7709ece9.Emscripten.clang

ExtendableError: Unknown network “ganache”. See your Truffle configuration file for available networks.
at Object.validateNetworkConfig (C:\Users\alanm\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\environment\environment.js:111:1)
at Object.detect (C:\Users\alanm\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\environment\environment.js:16:1)
at C:\Users\alanm\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:206:1

I have updated the network in truffle-config.js to the one ganache shows, still no luck. this has stumped me for about 24 hours now.

Hi @okcrypto

Are you running ganache while migrating the project?
Also make sure to link you truffle-config file to Ganache:

If you have already done the steps above, please post your truffle-config file and a screenshot of you Ganache with the information in my screenshot below:

Happy learning,
Dani

Here is my solution to owner test assignment:

it("should not allow not-owner to delete person", async function(){
    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 allow owner to delete person", async function(){
    let instance = await People.deployed();
    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
    await truffleAssert.passes(
      instance.deletePerson(accounts[0], {from: accounts[0]})
      , truffleAssert.ErrorType.REVERT
    );
  });
1 Like

I was using quick start in Ganache, as soon as I switched to make my own work everything worked! I had already updated the truffle-config.js and had it laded in Ganache but for some reason it didn’t want to work in Quickstart, when I created my own network and uploaded the truffle-config.js with the amended Development path and then in the truffle console ran migrate all the contracts compiled and deployed successfully. I don’t know how much longer it would have taken me to figure that out without you responding to my post. I appreciate it, Very happy to move forward. All I can do is laugh that it was something so simple.

1 Like

My solution to the problem. I peeked a bit in the forum, although I did the “meat” of the code myself :slight_smile: It says the test passed, but is it okay, if I did it without parseFloat?

const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");

contract("People", async function(accounts){

  beforeEach(async function(){

  });

  it("should not create a person with age over 150 yrs", async function(){
    let instance = await People.deployed();
    await truffleAssert.fails(instance.createPerson("Ziga", 270, 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("Ziga", 27, 190, {value: 1000000}), truffleAssert.ErrorType.REVERT);
  });
  it("should set senior status correctly", async function(){
    let instance = await People.deployed();
    await instance.createPerson("Ziga", 75, 190, {value: web3.utils.toWei("1", "ether")});
    let result = await instance.getPerson();
    assert(result.senior === true, "Senior level not set correctly");
  });
  it("should allow owner to delete an account", async function(){
    let instance = await People.deployed();
    await instance.createPerson("JohnyBravo", 29, 190, {value: web3.utils.toWei("1", "ether")});
    await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
  });
  it("should not allow non-owners to delete an account", async function(){
    let instance = await People.deployed();
    await instance.createPerson("BravoJohnny", 92, 190, {value: web3.utils.toWei("1", "ether")});
    await truffleAssert.fails(instance.deletePerson(accounts[2], {from: accounts[1]}));
  });
  it("should increase balance of the contract when a new user is added", async function(){
    let instance = await People.new();
    await instance.createPerson("Johnny", 29, 190, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
    let contractBalance = await web3.eth.getBalance(instance.address);
    assert(contractBalance === web3.utils.toWei("1", "ether"));
  });
  it("should allow owner to withdraw funds correctly and balance reduced to 0", async function(){
    let instance = await People.new();
    let ownerBalance = await web3.eth.getBalance(accounts[0]);
    await instance.createPerson("John", 31, 188, {from: accounts[1], value: web3.utils.toWei("5", "ether")});
    await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
    let newOwnerBalance = await web3.eth.getBalance(accounts[0]);
    assert(newOwnerBalance > ownerBalance, "Balance does not increase");
    let contractBalance = await web3.eth.getBalance(instance.address);
    assert(contractBalance == 0, "Balance should be zero");
  });
});

1 Like

Value Assignment solution:

it("should increase contract balance when adding a person", async function(){
    let balanceBefore = await web3.eth.getBalance(instance.address);
    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
    let balanceAfter = await web3.eth.getBalance(instance.address);
    let balanceChange = balanceAfter - balanceBefore;
    assert(balanceChange == web3.utils.toWei("1", "ether"), "Value doesn't transfer to contract");
  });

  it("should modify 'balance' variable correctly when adding a person", async function(){
    let instance = await People.new();
    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
    let balanceBN = await instance.balance();
    let balance = balanceBN.toString();
    assert(balance === web3.utils.toWei("1", "ether"), "'balance' variable not modified correctly")
  });

  it("should not allow not-owner to withdraw", async function(){
    await truffleAssert.fails(
      instance.withdrawAll({from: accounts[1]})
      , truffleAssert.ErrorType.REVERT
    );
  });

  it("should allow owner to withdraw", async function(){
    await truffleAssert.passes(
      instance.withdrawAll({from: accounts[0]})
      , truffleAssert.ErrorType.REVERT
    );
  });

  it("should clear contract balance when withdrawing", async function(){
    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
    let balance = await web3.eth.getBalance(instance.address);
    assert(balance != 0, "Current balance should not be 0");
    await instance.withdrawAll({from: accounts[0]});
    balance = await web3.eth.getBalance(instance.address);
    assert(balance == 0, "Current balance should be 0");
  });

  it("should clear 'balance' variable when withdrawing", async function(){
    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
    let balanceBN = await instance.balance();
    let balance = balanceBN.toString();
    assert(balance !== "0", "'balance' should not be 0");
    await instance.withdrawAll({from: accounts[0]});
    balanceBN = await instance.balance();
    balance = balanceBN.toString();
    assert(balance === "0", "'balance' should be 0");
  });

  it("should increase owner's balance when withdrawing", async function(){
    let instance = await People.new();
    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
    let ownerBalance = await web3.eth.getBalance(accounts[0]);
    await instance.withdrawAll({from: accounts[0]});
    let ownerBalanceAfter = await web3.eth.getBalance(accounts[0]);
    assert(ownerBalanceAfter > ownerBalance, "Owner's balance does not change correctly");
  });

In the process I ran into a problem though.

Initially for checking the owner’s balance change I wanted to use this function:

  it("should change owner's balance correctly when withdrawing", async function(){
    let instance = await People.new();
    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
    // mark 1
    let balance = await web3.eth.getBalance(instance.address);
    let ownerBalance = await web3.eth.getBalance(accounts[0]);
    let txnReceipt = await instance.withdrawAll({from: accounts[0]});
    // mark 2
    let gasUsed = txnReceipt.receipt.gasUsed;
    let ownerBalanceAfter = await web3.eth.getBalance(accounts[0]);
    let ownerBalanceChange = ownerBalanceAfter - ownerBalance;
    let expectedBalanceChange = balance - (gasUsed * 20000000000);
    assert(ownerBalanceChange === expectedBalanceChange, "Owner's balance does not change correctly");
  });

It was failing initially and so I examined it deeper. What I found out is that when I ran the test with the accounts balance at 100ETH it fails 100% of times, until the balance goes down to about 72 ETH. Then it starts failing only every fourth time. When the balance goes down to around 35ETH the test passes 100% of times.

The test fails due to a difference between ownerBalanceChange and expectedBalanceChange. The responsible amount appears in the place marked “// mark 2” and it is usually between -8200 and 8200 wei, one time reaching 160 * 10^9.

Any ideas what that might be?

1 Like

I thought i had a mistake at code level, but then after checking filip’s answer i realised it has to be something else. Can anybody give me a hint ? i leave my code + filip’s code in the comments, and below it the error i get:

Hey @ZigaP

but is it okay, if I did it without parseFloat ?

Solidity does not handle floating point numbers, indeed you always use unsigned integers. (integer >= 0).
Where did you want to use floats?

Happy learning,
Dani

Hi @lopotras

Javascript produces error when the numbers are too big.
Try this:

const a = 1000000000000000000 * 2
const b = 1000000000000000000 *2 - 1

console.log( a == b);

You would expect to be false, right :slight_smile: ?

A solution could be to use BigNumber.js

const BigNumber = require('bignumber.js');

const x = new BigNumber(10 ** 18);
const y = new BigNumber(10 ** 18 + 1);

console.log(x == y)

I like that you noticed it.
Never stop being curios.

Cheers,
Dani

1 Like

Hey @ArgenFrank

Can you please console.log(accounts[1]) and show the result?
Is the result is undefined post the whole test file.

cheers,
Dani

Hi dani, indeed its undefined:
image

Here is the code (its a screeshot bc of the vm):

ps: All the other tests run smoothly

i dont know why i had the parameter “network” and that caused my error. I must have been tired when coded it ahha

1 Like

Thanks @dan-i,

I tried it out, changing all the values to BigNumber. What finally got the error to go away was using BigInt() on top of that in the assert statement:

assert(BigInt(ownerBalanceChange) === BigInt(expectedBalanceChange), "Owner's balance does not change correctly");

Now it’s all running smoothly :+1:

1 Like

My code is quite verbose but it helped me think through the solution.

// TEST 06
    it("should allow the owner to delete people", async function () {
        let instance = await People.deployed();
        await instance.createPerson("Lisa", 35, 160, { from: accounts[6], value: web3.utils.toWei("1", "ether") });
        await truffleAssert.passes(instance.deletePerson(accounts[1], { from: accounts[0] }));
    });
    // TEST 07
    it("Contract balance should increase by 1 ether", async function () {
        let instance = await People.deployed();
        let contractBalanceBefore = await web3.eth.getBalance(instance.address);
        let price = web3.utils.toWei("1", "ether");
        await instance.createPerson("Susan", 31, 155, { from: accounts[0], value: price });
        let contractBalanceAfter = await web3.eth.getBalance(instance.address);
        assert((contractBalanceAfter - contractBalanceBefore >= price), "Balance didn't increase by >= 1 ether");
    });
    // TEST 08
    it("non owner should not be able to withdraw all funds from contract", async function () {
        let instance = await People.deployed();
        let contractBalanceBefore = await web3.eth.getBalance(instance.address);
        let accountBalanceBefore = await web3.eth.getBalance(accounts[0]);
        await truffleAssert.fails(instance.withdrawAll({from: accounts[9]}), truffleAssert.ErrorType.REVERT);
        let contractBalanceAfter = await web3.eth.getBalance(instance.address);
        let accountBalanceAfter = await web3.eth.getBalance(accounts[0]);
        assert(contractBalanceAfter > 0, "contract balance should be non-zero");
    });
    // TEST 09
    it("only owner should be able to withdraw all funds from contract", async function () {
        let instance = await People.deployed();
        let contractBalanceBefore = await web3.eth.getBalance(instance.address);
        let accountBalanceBefore = await web3.eth.getBalance(accounts[0]);
        await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}), truffleAssert.ErrorType.REVERT);
        let contractBalanceAfter = await web3.eth.getBalance(instance.address);
        let accountBalanceAfter = await web3.eth.getBalance(accounts[0]);
        assert(contractBalanceAfter == 0, "contract balance should be zero");
    });
1 Like

Owner Test Assignment

const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");

contract("People", async function(accounts){
  it("shouldn't create a persion with age above 150", 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", 50, 190, {value: 1000}), truffleAssert.ErrorType.REVERT);
  })
  it("should set senior status correctly", async function(){
    let instance = await People.deployed();
    await instance.createPerson("Superman", 90, 190, {value: web3.utils.toWei("1", "ether")});
    let boolean = await instance.getPerson();
    assert(boolean.senior === true, "Senior level has been set.");
  })
  it("shouldn't delete the person.", async function(){
    let instance = await People.deployed();
    await instance.createPerson("Superman", 42, 190, {value: web3.utils.toWei("1", "ether"), from: accounts[4]});
    await truffleAssert.fails(instance.deletePerson(accounts[4], {from: accounts[2]}), truffleAssert.ErrorType.REVERT);
  })
  it("should delete the person.", async function(){
        let instance = await People.deployed();
        await instance.createPerson("Batman", 45, 187, {value: web3.utils.toWei("1","ether"), from: accounts[1]});
        await truffleAssert.fails(instance.deletePerson(accounts[1], {from: accounts[0]}), truffleAssert.ErrorType.REVERT);
    });

});
1 Like

VALUE ASSIGNMENT TEST (first test)

here is my code for checking if the balance of the contract matches the balance on the blockchain, and that it equals 1 ether:

   it("should increase the contract balance when person is created", async function(){
let instance = await People.new();
await instance.createPerson("Bob", 65, 190, {from: accounts[1], value: web3.utils.toWei("1", "ether")});

let newBalance = await instance.balance();
let blockchainBalance = await web3.eth.getBalance(instance.address);

assert(newBalance == blockchainBalance, "Contract balance does not match Blockchain balance");
assert(newBalance == web3.utils.toWei("1", "ether"));
})

I saw in Filip’s solution, he used parseFloat.
Mine worked without using it, so why would he use it?
Maybe I just don’t understand when/why to use parseFloat.

1 Like