Unit Testing in Truffle

Value assignment:

it("should increase the balance of the contract when a new person is created", async function(){
        instance = await People.deployed();
        let balanceBefore = await instance.balance();        
        await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
        let balanceAfter = await instance.balance();
        assert(parseInt(balanceAfter) === (parseInt(balanceBefore) + parseInt(web3.utils.toWei("1", "ether"))));
    });

    it("should match balance on the contract with the balance for the contract address on the blockchain", async function(){
        instance = await People.deployed();
        let balance = await instance.balance();        
        let balanceOnBlockChain = await web3.eth.getBalance(instance.address)
        assert (parseInt(balance) === parseInt(balanceOnBlockChain));
    });


    it("shouldn't let non owner withdraw", async function(){
        let instance = await People.deployed();
        await truffleAssert.fails(instance.withdrawAll({from: accounts[1]}));
    });

    it("should allow the contract owner to withdraw", async function(){
        let instance = await People.deployed();
        await truffleAssert.passes(instance.withdrawAll());
        let balance = await instance.balance();
        assert(parseInt(balance) === 0);
    });
1 Like

thanks a lot to you both!!1 but the same error appears! … If this continues to happen, is it wise to continue? … I have always managed to move forward, but Im stuck in here for more than1 week now… And given the fact that Im paying on a monthly basis … time is money … please help @dan-i … I changed the name to People and Peoplecontract and it doesnt work (on the importing part as well as in the function call)

Hey @javier_ortiz_mir

The wrong deployment was an issue, what is the error message now?
Please let me know :slight_smile:

All screenshots are welcome

Cheers!

it is the same as in my original post :cry:

Please push on github and share the link, thanks!

@dan-i if you’d like a screenshot:

1 Like

I still dont know how to do that :sweat_smile:

Hey @javier_ortiz_mir

This is a different error than what stated before ReferenceError: People is not defined that was indeed related to the wrong contract declaration :slight_smile:

To fix this one you have to downgrade node as explained here: FAQ - How to downgrade Node.Js

Cheers,
Dani

1 Like

it(“shouldn’t destroy Bob”, async function(){
let instance = await People.deployed();
await truffleAssert.fails(instance.deletePerson(accounts[0], {from: accounts[1]}));
});

1 Like

Sorry no idea where to begin with testing the balances…

I am receiving an error mentioning Error: Truffle is currently using solc 0.5.8, but one or more of your contracts specify "pragma solidity 0.5.12". Please update your truffle config or pragma statement(s).

1 Like

A bit difficult that for the novices …

  let balanceBefore = parseFloat(awaitweb3.eth.getBalance(accounts[0]));
//parseFloat i would of never of got....... Never even heard of it :L.   
await instance.withdrawAll();
  let balanceAfter = parseFloat(await web3.eth.getBalance(accounts[0]));
  assert(balanceBefore < balanceAfter, "Owners balance was not increased after withdrawal");

Hi mate, i had the same issue and you can edit the Compiler in youre truffle.config.js file ;).

2 Likes
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", 150, 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", 37, 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("Verifies if other account besides the Owner can delete a Person", async function(){
        let instance = await People.deployed();
        await instance.createPerson("Mitnick", 50, 190, {from: accounts[1], value: web3.utils.toWei("1", "ether") });
        await truffleAssert.fails(instance.deletePerson(accounts[1], {from: accounts[1]}), truffleAssert.ErrorType.REVERT);
    });
    it("Verifies if the Owner can delete a Person", async function(){
        let instance = await People.deployed();
        await instance.createPerson("Filipe", 37, 180, {from: accounts[1], value: web3.utils.toWei("1", "ether") });
        await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[1]}));
    });
});
1 Like

Hey @kryptokrymmenos

You can follow this: FAQ - How do change truffle version

:cupid: dani!!!1!!!1 finally!!! thanks a loooooooooooooooooooooooot

2 Likes

Owner Test Assignment:

it(“should only delete by owner”, async function(onlyOwner)
{
let instance = await People.deployed();
assert(msg.sender==onlyOwner,“Only owner can delete”);
})

My best guess before looking at the solution but will definitely improve once I see the solution and grow my knowledge with it.

Hi guys, there you go

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

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, 170, {value: web3.utils.toWei('1', 'ether')}),
            truffleAssert.ErrorType.REVERT
        );
    });

    it('should not create a person without correct amount of payment', async function() {
        let instance = await People.deployed();
        await truffleAssert.fails(
            instance.createPerson('Bob', 50, 170, {value: 1000}), 
            truffleAssert.ErrorType.REVERT
        );
    });

    it('should set senior status correctly', async function() {
        let instance = await People.deployed();
        await instance.createPerson('Bob', 65, 170, {value: web3.utils.toWei('1', 'ether')});
        let person = await instance.getPerson();
        assert.equal(person.senior, true, 'status should be senior if age is equal to or over 65 years old');
    });

    it('should not allow non-owner to delete people', async function() {
        let instance = await People.deployed();
        await instance.createPerson('Bob', 65, 170, {value: web3.utils.toWei('1', 'ether'), from: accounts[1]});            
        await truffleAssert.fails(
            instance.deletePerson(accounts[1], { from: accounts[1] }),
            truffleAssert.REVERT
        );
    });
});

Hi guys,

Here I post my solution. It works.

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

contract('People', async function(accounts) {
    let instance;

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

   ... 

    it('should increase contract balance when create person', async function() {
        instance = await People.new();  // create brand new copy of contract
        await instance.createPerson('Bob', 65, 170, {value: web3.utils.toWei('1', 'ether'), from: accounts[3]});
        let bal = await web3.eth.getBalance(instance.address);
        assert.equal(bal, web3.utils.toWei('1', 'ether'), 'each successful create person will add balance to contract');
    });

    it('should NOT allow non-owner to withdraw balance from contract', async function() {
        await truffleAssert.fails(
            instance.withdrawAll({from: accounts[1]}),
            truffleAssert.REVERT
        );
    });

    it('should only allow owner to withdraw balance from contract', async function() {
        let balBfrWithdraw = await web3.eth.getBalance(accounts[0]);

        await truffleAssert.passes(
            instance.withdrawAll({from: accounts[0]})
        );
        
        let balAftWithdraw = await web3.eth.getBalance(accounts[0]);

        assert(balAftWithdraw > balBfrWithdraw, 'owner account balance should increase in ether');
    });
});
1 Like

Value Assignment Attempt:

it(“should update balance of contract address when person added”, async function()
{
await instance.createPerson(“Lisa”, 35, 160,{from:accounts[1],value:web3.utils.toWei(“1”,“ether”)});
assert(balance===web.eth.getBalance(contract address), “balance not updated”)
});
it(“should update balance of owner address after contract address funds withdrawn”, async function()
{
await instance.createPerson(“Bob”, 35, 160,{from:accounts[1],value:web3.utils.toWei(“1”,“ether”)});
await instance.withdrawAll({from:accounts[0]})
mgs.value+=toTransfer
assert(balance==0, “balance not withdrawn”)
});