Unit Testing in Truffle

Can you send upload your code on GitHub and send me link?

We will sort this out :slight_smile:

2 Likes

https://github.com/Icarus-6/An-issue-with-tests-in-Truffle

Thanks for your help bro, there might be an issue in peopletest.js or people.sol

1 Like

@doctormartin67
Yeah you are right the solidity creates a getter function for public variables :slight_smile:

1 Like
  it("should update balance correctly", async function(){
    let instance=await People.new();
    await instance.createPerson("Bob",66,190, {value: web3.utils.toWei("1","ether")});

    let balance=await instance.balance(); //instance.balance is the contract's balance
    let balanceNumber=parseInt(balance); //Converting balance above to a floating point number

    let realBalance=await web3.eth.getBalance(instance.address); //this retrieves the balance in an address. Not the same as line 56

    assert(balanceNumber=web3.utils.toWei("1","ether") && realBalance==balanceNumber);
  });
  it("owner should be able to withdraw", async function(){
    let instance=await People.new();
    await instance.createPerson("Bob",66,190, {value: web3.utils.toWei("1","ether")});
    await truffleAssert.passes(instance.withdrawAll({from:accounts[0]}));
  });
  it("non-owner should not be able to withdraw", async function(){
    let instance=await People.new();
    await instance.createPerson("Bob",66,190, {value: web3.utils.toWei("1","ether")});
    await truffleAssert.fails(instance.withdrawAll({from:accounts[2]},truffleAssert.ErrorType.REVERT));
  });
  it("should reset balance to zero after withdrawal", async function(){
    let instance=await People.new();
    await instance.createPerson("Bob",66,190, {value: web3.utils.toWei("1","ether")});

    await instance.withdrawAll();

    let balance=await instance.balance(); //instance.balance is the contract's balance
    let balanceNumber=parseInt(balance); //Converting balance above to a floating point number

    let realBalance=await web3.eth.getBalance(instance.address); //this retrieves the balance in an address. Not the same as line 56

    assert(balanceNumber=web3.utils.toWei("0","ether") && realBalance==balanceNumber);
  });
1 Like

Hi @Ondrej.S
Apologies for late reply!
You will have you install nvm and also install node js 10
There are few compatibility issues with node v12 (and above ) and truffle.
It working fine for me with node v9
check this out: https://www.sitepoint.com/quick-tip-multiple-versions-node-nvm/

Then switch to node version 10 by using nvm use 10

Let me know if it doesnā€™t work, there is one more way :slight_smile:

1 Like

Thank you! The older version of node works fine :slight_smile:

1 Like

Owner 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()
  });
  it("should not allow non-owner to delete people", async function(){
    let instance = await People.deployed();
    await instance.createPerson("Andrew", 25, 180, {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("Andrew", 25, 180, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
    await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
  });
});

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()
  });
  it("should increase the balance by 1 ETH", async function(){
    let instance = await People.deployed();
    let balance = await web3.eth.getBalance(People.address);
    await instance.createPerson("Andrew", 25, 180, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
    let newBalance = await web3.eth.getBalance(People.address);
    assert(newBalance == parseInt(Balance) + parseInt(web3.utils.toWei("1", "ether")), "The funds weren't trasferred");
    });

  it("shouldn't be possible to withdraw funds unless you're the contract owner", async function(){
    let instance = await People.deployed();
    await truffleAssert.fails(instance.withdrawAll({from: accounts[1]}));
    });
  it("shoud be possible only for the contract owner to withdraw funds", async function(){
    let instance = await People.deployed();
    await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
    });
});
1 Like
   it("Delete can be performed by the owner", async function() {
     let instance  = await PeopleWorld.deployed();

     await instance.createPerson("Moby", 25, 170, {value: web3.utils.toWei("1","ether"), from: accounts[2]});
     let person = await instance.getPerson({from: accounts[2]});
     assert (person.name==="Moby", "Person was not inserted");

     await truffleAssert.passes(instance.deletePerson(accounts[2], {from: accounts[0]}));
     person = await instance.getPerson({from: accounts[2]});
     assert(person.name==="", "Person was not deleted")
   });

   it("Delete cannot be performed by a non owner", async function() {
     let instance  = await PeopleWorld.deployed();

     await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1","ether"), from: accounts[2]});

     await truffleAssert.fails(instance.deletePerson(accounts[2], {from: accounts[2]}), truffleAssert.ErrorType.REVERT);
   });
1 Like
 it("Withdraw funds cannot be performed by a non owner", async function() {
    let instance  = await PeopleWorld.new();

    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1","ether"), from: accounts[2]});

    await truffleAssert.fails(instance.withdrawAll({from: accounts[2]}), truffleAssert.ErrorType.REVERT);

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

    assert(parseFloat(balance)!=web3.utils.toWei("0","ether"),"The balance has been withdrawn");
    assert(contractBalance!=web3.utils.toWei("0","ether"),"The contract balance has been withdrawn");
  });

  it("Check the balance can be withdrawn", async function() {
    let instance  = await PeopleWorld.new();

    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1","ether"), from: accounts[5]}); 
    let priorOwnerBalance = await web3.eth.getBalance(accounts[0]);

    await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));

    let currentOwnerBalance = await web3.eth.getBalance(accounts[0]);
    let contractBalance = await web3.eth.getBalance(instance.address);
    let balance = await instance.balance();

    assert(priorOwnerBalance<currentOwnerBalance,"Owner balance did not increase");
    assert(parseFloat(balance)==web3.utils.toWei("0","ether"),"The balance has not been withdrawn");
    assert(contractBalance==web3.utils.toWei("0","ether"),"The contract balance has not been withdrawn");
  });
1 Like

Owner Test Assignment
Testing the onlyOwner function modifier

it("If is not owner cannot delete a person",async function(){

        let instance = await People.deployed()

        await truffleAssert.fails(instance.deletePerson(accounts[0],{ from: accounts[1]}),truffleAssert.ErrorType.REVERT)

    })

    it("Onwer should delete a person",async function(){

        let instance = await People.deployed()

        await instance.deletePerson(accounts[0], { from: accounts[0] })

        let result = await instance.getPerson({ from: accounts[0] })

        assert(result.name === "" && result.age == 0, "Person not deleted by the owner")

    })

My solution for checking:
1.- Banlance is increassing
2.- If is not owner cannot withdraw
3.- Owner is able to withdraw

it("Balance should increasse when creating a person", async function(){

        let instance = await People.deployed();

        let Balance = await web3.eth.getBalance(People.address);

        await instance.createPerson("Monkey", 31, 130, {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")), "Tranfer of the amount fails");        

    });

    it("If is not owner cannot withdraw", async function(){

        let instance = await People.deployed();        

        await truffleAssert.fails(instance.withdrawAll({ from: accounts[1]}),truffleAssert.ErrorType.REVERT)

        let Balance = await web3.eth.getBalance(People.address);

        assert(Balance != 0,"Anyone is able to withdraw")

    });

    it("Only owner is able to withdraw", async function(){

        let instance = await People.deployed();        

        let Balance = await web3.eth.getBalance(People.address);

        await instance.withdrawAll({ from: accounts[0]})

        let newBalance = await web3.eth.getBalance(People.address);

        assert(newBalance == 0 && Balance > 1, "Withdaw by owner fails");        

    });

    it("If is not owner cannot withdraw", async function(){

        let instance = await People.deployed();

        let Balance = await web3.eth.getBalance(People.address);

        await truffleAssert.fails(instance.withdrawAll({ from: accounts[1]}),truffleAssert.ErrorType.REVERT)

        let newBalance = await web3.eth.getBalance(People.address);

        assert(newBalance != 0,"Anyone is able to withdraw")

    });

    it("Only owner is able to withdraw", async function(){

        let instance = await People.deployed();        

        await instance.withdrawAll({ from: accounts[0]})

        let newBalance = await web3.eth.getBalance(People.address);

        assert(newBalance == 0, "Did not transfer the estimated funds");        

    });

Owner Test Assignment solution.
This took me a little while to figure out exactly what I could get to work and test for but I am fairly confident with this now. Going over and over the code many times really helped me wrap my head around it all. I have included many deletion tests from owners, non-owners and new owners as well.

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

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

     it("Should allow owner to create and delete people correctly", async function(){
        let instance = await OwnableTest.deployed();
        let owner1 = accounts[0];
        let owner2 = accounts[1];

        await instance.createPerson("Alice", 40, 160, 65, {value: web3.utils.toWei("1", "ether"), from: owner1});
        await instance.createPerson("Bob", 60, 170, 70, {value: web3.utils.toWei("1", "ether"), from: owner2});

        let createdPerson = await instance.getPerson();
        assert(createdPerson.name === "Alice", "Getting the created Person has failed");

        await instance.deletePerson(owner1);
        createdPerson = await instance.getPerson();
        assert(createdPerson.name === "", "Person wasn't deleted properly");

        createdPerson = await instance.getPerson({from: owner2});
        assert(createdPerson.name === "Bob", "Getting the created Person has failed");

        await instance.deletePerson(owner2);
        createdPerson = await instance.getPerson({from: owner2});
        assert(createdPerson.name === "", "Person wasn't deleted properly");
    });


    it("Shouldn't allow owner 1's person to be deleted by owner 2", async function(){
        let instance = await OwnableTest.deployed();
        let owner1 = accounts[0];
        let owner2 = accounts[1];
        let owner3 = accounts[2];

        await instance.createPerson("Alice", 40, 160, 65, {value: web3.utils.toWei("1", "ether"), from: owner1});
        await instance.createPerson("Bob", 60, 170, 70, {value: web3.utils.toWei("1", "ether"), from: owner2});
        await instance.createPerson("Charlie", 80, 180, 75, {value: web3.utils.toWei("1", "ether"), from: owner3});

        await truffleAssert.fails(instance.deletePerson(owner1, {from: owner2}), truffleAssert.ErrorType.REVERT);
    });


    it("Shouldn't allow owner 2's person to be deleted by owner 3", async function(){
        let instance = await OwnableTest.deployed();
        let owner1 = accounts[0];
        let owner2 = accounts[1];
        let owner3 = accounts[2];

        await instance.createPerson("Alice", 40, 160, 65, {value: web3.utils.toWei("1", "ether"), from: owner1});
        await instance.createPerson("Bob", 60, 170, 70, {value: web3.utils.toWei("1", "ether"), from: owner2});
        await instance.createPerson("Charlie", 80, 180, 75, {value: web3.utils.toWei("1", "ether"), from: owner3});

        await truffleAssert.fails(instance.deletePerson(owner2, {from: owner2}), truffleAssert.ErrorType.REVERT);
    });



    it("Shouldn't allow owner 3's person to be deleted by owner 1 after ownership change", async function(){
        let instance = await OwnableTest.deployed();
        let owner1 = accounts[0];
        let owner2 = accounts[1];
        let owner3 = accounts[2];

        await instance.createPerson("Alice", 40, 160, 65, {value: web3.utils.toWei("1", "ether"), from: owner1});
        await instance.createPerson("Bob", 60, 170, 70, {value: web3.utils.toWei("1", "ether"), from: owner2});
        await instance.createPerson("Charlie", 80, 180, 75, {value: web3.utils.toWei("1", "ether"), from: owner3});

        await instance.newOwner(owner2);

        await truffleAssert.fails(instance.deletePerson(owner3, {from: owner1}), truffleAssert.ErrorType.REVERT);
    });


    it("Should allow owner 2's person to be deleted by owner 3 after ownership change", async function(){
        let instance = await OwnableTest.deployed();
        let owner1 = accounts[0];
        let owner2 = accounts[1];
        let owner3 = accounts[2];

        await instance.createPerson("Alice", 40, 160, 65, {value: web3.utils.toWei("1", "ether"), from: owner1});
        await instance.createPerson("Bob", 60, 170, 70, {value: web3.utils.toWei("1", "ether"), from: owner2});
        await instance.createPerson("Charlie", 80, 180, 75, {value: web3.utils.toWei("1", "ether"), from: owner3});

        await instance.newOwner(owner3, {from: owner2});

        await truffleAssert.passes(instance.deletePerson(owner2, {from: owner3}), truffleAssert.ErrorType.REVERT);
    });

})
1 Like

Unfortunately I saw solution for this assignment when I was comparing my try of Owner test assignment with final codeā€¦after that I couldā€™t do anything else but replicate codeā€¦ so itā€™s not my solution and therefore I will not post anything for this assignment until I came up with some alternative solution on my own. :face_with_raised_eyebrow:

1 Like

Managed to clean up the code after learning the beforeEach functionality

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

contract ("OwnableTest", async function(accounts){
    let instance;
    let owner1;
    let owner2;
    let owner3;


    before(async function(){
        instance = await OwnableTest.new();
        owner1 = accounts[0];
        owner2 = accounts[1];
        owner3 = accounts[2];

        await instance.createPerson("Alice", 40, 160, 65, {value: web3.utils.toWei("1", "ether"), from: owner1});
        await instance.createPerson("Bob", 60, 170, 70, {value: web3.utils.toWei("1", "ether"), from: owner2});
        await instance.createPerson("Charlie", 80, 180, 75, {value: web3.utils.toWei("1", "ether"), from: owner3});
    });


    it("Should allow owner to create and delete people correctly", async function(){
        let createdPerson = await instance.getPerson();
        assert(createdPerson.name === "Alice", "Getting the created Person has failed");

        await instance.deletePerson(owner1);
        createdPerson = await instance.getPerson();
        assert(createdPerson.name === "", "Person wasn't deleted properly");

        createdPerson = await instance.getPerson({from: owner2});
        assert(createdPerson.name === "Bob", "Getting the created Person has failed");

        await instance.deletePerson(owner2);
        createdPerson = await instance.getPerson({from: owner2});
        assert(createdPerson.name === "", "Person wasn't deleted properly");
    });

    it("Shouldn't allow owner 1's person to be deleted by owner 2", async function(){
        await truffleAssert.fails(instance.deletePerson(owner1, {from: owner2}), truffleAssert.ErrorType.REVERT);
    });


    it("Shouldn't allow owner 1's person to be deleted by owner 3", async function(){
        await truffleAssert.fails(instance.deletePerson(owner1, {from: owner2}), truffleAssert.ErrorType.REVERT);
    });


    it("Shouldn't allow owner 2's person to be deleted by owner 2", async function(){
        await truffleAssert.fails(instance.deletePerson(owner2, {from: owner2}), truffleAssert.ErrorType.REVERT);
    });


    it("Shouldn't allow owner 2's person to be deleted by owner 3", async function(){
        await truffleAssert.fails(instance.deletePerson(owner2, {from: owner2}), truffleAssert.ErrorType.REVERT);
    });



    it("Shouldn't allow owner 3's person to be deleted by owner 1 after ownership change", async function(){
        await instance.newOwner(owner2);
        await truffleAssert.fails(instance.deletePerson(owner3, {from: owner1}), truffleAssert.ErrorType.REVERT);
        await truffleAssert.passes(instance.deletePerson(owner3, {from: owner2}), truffleAssert.ErrorType.REVERT);
    });


    it("Should allow owner 2's person to be deleted by owner 3 after ownership change", async function(){
        await instance.newOwner(owner3, {from: owner2});
        await truffleAssert.passes(instance.deletePerson(owner2, {from: owner3}), truffleAssert.ErrorType.REVERT);
    });
})

1 Like

@MrGanD
Thanks for reaching out!
Its completely fine to replicate code until you understand it, you can try more assignments for which solutions are not available :slight_smile: so there is no possibility of replication.

2 Likes
it("makes sure the deleting is made correct", async function(){
	let instance = await People.deployed();
	await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
	

	});
	it("makes sure only the owner can delete the person", async function(){
	let instance = await People.deployed();	
	await instance.createPerson("Jon", 29, 170, {value: web3.utils.toWei("1", "ether")});
	await truffleAssert.fails(instance.deletePerson(accounts[2], {from: accounts[2]}, truffleAssert.ErrorType.REVERT));
	
	});
});
1 Like

Value assignment
The following points were the required tasks which I have accomplished in the code below

  1. Balance of contract is increased when person is created
  2. Balance of contract matches contract balance on blockchain
  3. That the owner can withdraw the amount
  4. Balance of contract after withdrawal is 0
  5. New balance of contract matches contract balance on blockchain

This task broke my brain a little as I spent hours trying to get a little cutesy with the code and introduce a few extra problems, such as: having balances for the users who were creating people and a balance for the contract owner and to test for the change in balance after the creation of people or the withdrawal of people, etc.
This gave me so many headaches as the amounts are forever changing with each test run unless I reset the contract in ganache and reset the migration in truffle everytimeā€¦painful!!!
Decided after hours of torture to scrap these tests as I could not figure out a way to show the decreased amount correctly, just a vague test that it had been decreased from the original 100 ether starting amount for a fresh contract. I then figured there was probably a reason that Filip decided not to ask for these tests also.

Anyways, had a lot of fun working this one out. A little bit of brain strain for a beginner like me but a fantastic challenge!!

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

contract ("Balances", async function(accounts){
    let instance;
    let contractAddess;
    let contractBalance;
    let origContractBalance;

    const contractOwner = accounts[0];
    const user1 = accounts[1];
    const user2 = accounts [2];

    const ether_0 = await web3.utils.toWei("0", "ether");
    const ether_1 = await web3.utils.toWei("1","ether");


    beforeEach(async function(){
        instance = await Balances.new();
        origContractBalance = ether_0;
    });

    it("Balance of Contract should be equal to 0 at start of contract", async function(){
        contractBalance = await web3.eth.getBalance(instance.address);
        assert(contractBalance === ether_0, "Contract balance is not 0 ether at contract start");
        assert(contractBalance === origContractBalance, "Contract balance is not 0 ether at contract start");
    });

    it("Users should be able to create people", async function(){
        await truffleAssert.passes(instance.createPerson("Alice", 40, 160, 65, {value: ether_1, from: user1}), truffleAssert.ErrorType.REVERT);
        let user1Person = await instance.getPerson({from: user1});
        assert(user1Person.name == "Alice", "User 1's person 'Alice' was not created");

        await truffleAssert.passes(instance.createPerson("Bob", 60, 170, 70, {value: ether_1, from: user2}), truffleAssert.ErrorType.REVERT);
        let user2Person = await instance.getPerson({from: user2});
        assert(user2Person.name == "Bob", "User 2's person 'Bob' was not created");
    });

    it("Balance of contract should match the blockchain balance at 1 ether after person is created", async function(){
        await instance.createPerson("Bob", 60, 170, 70, {value: ether_1, from: user2});
        contractBalance = await web3.eth.getBalance(instance.address);
        assert(contractBalance === ether_1, "Contract balance is not 1 ether after creation of person");
    });

    it("Balance of contract should be equal to blockchain balance", async function(){
        await instance.createPerson("Bob", 60, 170, 70, {value: ether_1, from: user2});
        contractBalance = await web3.eth.getBalance(instance.address);
        assert(contractBalance === ether_1, "Contract balance is not 1 ether after creation of person");
    });

    it("Contract Owner can withdraw funds and contract balance will be 0 after withdrawal, which should match the blockchain balance", async function(){
        await truffleAssert.passes(instance.withdrawAll({from: contractOwner}), truffleAssert.ErrorType.REVERT);
        contractBalance = await web3.eth.getBalance(instance.address);
        assert(contractBalance === ether_0, "Contract balance is not set to 0");
    });

    it("Other users cannot withdraw funds unless ownership is changed to them", async function(){
        await truffleAssert.fails(instance.withdrawAll({from: user1}), truffleAssert.ErrorType.REVERT);
        await instance.newOwner(user2);
        await truffleAssert.passes(instance.withdrawAll({from: user2}), truffleAssert.ErrorType.REVERT);
    });

})

1 Like
const PeopleRegistry = artifacts.require("PeopleRegistry");
const assertions = require("truffle-assertions");

contract("PeopleRegistry", async function(accounts){
  it("should add new person to registry", async function(){
    let inst = await PeopleRegistry.deployed();
    inst.addPerson(1,1,"Rafael","Male", {value: web3.utils.toWei("1","ether")});
    let p = await inst.getPerson();
    assert.equal(p.name,"Rafael");
  });

  it("should require correct ether cost", async function(){
    let inst = await PeopleRegistry.deployed();
    await assertions.fails(inst.addPerson(0,0,"","",{value: web3.utils.toWei("0.9","ether")}));
  });

  it("shouldn't allow too high an age", async function(){
    let inst = await PeopleRegistry.deployed();
    await assertions.fails(inst.addPerson(151,0,"","",{value: web3.utils.toWei("1","ether")}));
  });

  it("should only allow owner to delete person", async function(){
    let inst = await PeopleRegistry.deployed();
    await assertions.fails(inst.deletePerson(0, {from: accounts[2]}));
  });
});

All tests are working correctly :smile:

1 Like
  it("should allow owner to withdraw balance", async function(){
    let gasPrice = 20000000000;
    let balance_0 = parseFloat(await inst.getBalance());
    let ownerWallet_0 = await web3.eth.getBalance(accounts[0]);
    let result = await inst.seize();
    let gasCost = result.receipt.gasUsed * gasPrice;
    let balance_1 = parseFloat(await inst.getBalance());
    let ownerWallet_1 = await web3.eth.getBalance(accounts[0]);
    assert(ownerWallet_1 - ownerWallet_0 - balance_0 + gasCost == 0 && balance_1 == 0, "Contract did not withdraw funds");

  });
1 Like

Update: after watching the solutions video and discovering the parseFloat function, I have now been able to get the tests to do pretty much what I was aiming for at the start of this (the headaches part). I also noticed above I have repeated a test with slightly different titles, I think I was intending to do something else there and copied code down to change and then forgot about it. Have now deleted it
See the tests that I amended below:

//This test was amended from just verifying that a person could be created
it("Users should be able to create people and their balance should be decreased", async function(){
        originalBalance = parseFloat(await web3.eth.getBalance(user1));
        await truffleAssert.passes(instance.createPerson("Alice", 40, 160, 65, {value: ether_1, from: user1}), truffleAssert.ErrorType.REVERT);
        let user1Person = await instance.getPerson({from: user1});
        assert(user1Person.name == "Alice", "User 1's person 'Alice' was not created");
        newBalance = parseFloat(await web3.eth.getBalance(user1));
        assert(originalBalance > newBalance, "User 1's balance was not decreased");

        originalBalance = parseFloat(await web3.eth.getBalance(user2));
        await truffleAssert.passes(instance.createPerson("Bob", 60, 170, 70, {value: ether_1, from: user2}), truffleAssert.ErrorType.REVERT);
        let user2Person = await instance.getPerson({from: user2});
        assert(user2Person.name == "Bob", "User 2's person 'Bob' was not created");
        newBalance = parseFloat(await web3.eth.getBalance(user2));
        assert(originalBalance > newBalance, "User 2's balance was not decreased");
    });

//This test was amended to show that the owner's balance increases after withdrawal.
it("Contract Owner can withdraw funds (and balance wil be increased) and contract balance will be 0 after withdrawal (which should match the blockchain balance)", async function(){
        await instance.createPerson("Alice", 40, 160, 65, {value: ether_1, from: user1});
        originalBalance = parseFloat(await web3.eth.getBalance(contractOwner));
        await truffleAssert.passes(instance.withdrawAll({from: contractOwner}), truffleAssert.ErrorType.REVERT);
        newBalance = parseFloat(await web3.eth.getBalance(contractOwner));
        contractBalance = await web3.eth.getBalance(instance.address);
        assert(contractBalance === ether_0, "Contract balance is not set to 0");
        assert(originalBalance < newBalance, "Owner's balance was not increased after withdrawal");
    });

//This test was amended to show that the new owners balance is increased when withdrawal occurs.
it("Other users cannot withdraw funds unless ownership is changed to them", async function(){
        await instance.createPerson("Alice", 40, 160, 65, {value: ether_1, from: user1});
        await truffleAssert.fails(instance.withdrawAll({from: user1}), truffleAssert.ErrorType.REVERT);
        await instance.newOwner(user2);
        originalBalance = parseFloat(await web3.eth.getBalance(user2));
        await truffleAssert.passes(instance.withdrawAll({from: user2}), truffleAssert.ErrorType.REVERT);
        newBalance = parseFloat(await web3.eth.getBalance(user2));
        assert(originalBalance < newBalance, "User 2's balance increased after change of ownership and subsequent withdrawal");
    });

Good timesā€¦

1 Like