Unit Testing in Truffle

once again I have problems following this course… @filip please your help:

A question before… If we are importing the contract People into test, shouldn ’ t we use the const Peoplecontract instead of “People”?

everything compiles and migrates fine, but when I do the test, i get the following error:

TypeError [ERR_INVALID_REPL_INPUT]: Listeners for uncaughtException cannot be used in the REPL
at process. (repl.js:302:15)
at process.emit (events.js:326:22)
at process.emit (/usr/lib/node_modules/truffle/build/webpack:/~/source-map-support/source-map-support.js:465:1)
at processEmit [as emit] (/usr/lib/node_modules/truffle/build/webpack:/~/signal-exit/index.js:155:1)
at _addListener (events.js:357:14)
at process.addListener (events.js:405:10)
at Runner.run (/usr/lib/node_modules/truffle/node_modules/mocha/lib/runner.js:868:11)
at Mocha.run (/usr/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:612:17)
at /usr/lib/node_modules/truffle/build/webpack:/packages/core/lib/test.js:128:1
at new Promise ()
at Object.run (/usr/lib/node_modules/truffle/build/webpack:/packages/core/lib/test.js:127:1)
at runMicrotasks ()
at processTicksAndRejections (internal/process/task_queues.js:93:5)

my code for the test file is as follows:

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

contract ("People", async function(){
  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")}));
  });
});

@filip … already fixed the previous error by downgrading node.js, but now I run the test and I get the following error:

Contract: People
1) shouldn’t create a person with age over 150 years
> No events were emitted

0 passing (42ms)
1 failing

  1. Contract: People
    shouldn’t create a person with age over 150 years:
    ReferenceError: People is not defined
    at Context. (test/peopletest.js:6:20)
    at web3.eth.getBlockNumber.then.result (/usr/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/testrunner.js:154:1)
    at process._tickCallback (internal/process/next_tick.js:68:7)

my code is exactly the same as th above message:

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

contract ("People", async function(){
  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")}));
  });
});

1- I did not understand this part of the code:

let balanceBefore = parseFloat(await web3.eth.getBalance(accounts[0]));
await instance.withdrawAll();
let balanceAfter = parseFloat(await web3.eth.getBalance(accounts[0]));
assert(balanceBefore < balanceAfter, “Owners balance was not increased after withdrawal”);

here the assert function is used to make sure that balanceBefore < balanceAfter, but how : when we execute the command: await instance.withrawAll(); this should withraw the total amount of money, which means the balance after the withdraw should be zero and hence : balanceBefore> balanceAfter.

2- I did not understand this as well : let balance = await instance.balance(); (we don’t have a function balance like createPerson(…) or deletePerson(…)in our code, we have only an uint balance, so how can we make : let balance = await instance.balance(); )

i have the wrong version of Truffle. this error is appearing…

to continue i need the correct solidity to compile…

1 Like

Hey @Rob_McCourt, hope you are good.

You just need to change your truffle compiler version, this can be done though the truffle-config.js file at the end of it.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

thank you kind sir, i changed the contract compiler version so i could move on…

All good pal.

Rob.

1 Like

Hi!

This is more of a general question. Why do we need to write “before” as shown:

let instance;


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

let instance

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

Wouldn’t the async function run once before all the tests anyways (since commands are executed line-by-line)? In other words, why can’t I write the command as shown below:

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

let instance;

instance = await People.deployed()

Thank you !!!

Hey all, ive came back to try and finish Testing on my People Contract.

I have tried to run Truffle Console and i receive this error message?

Could not find suitable configuration file.
Truffle v5.0.42 (core: 5.0.42)
Node v10.21.0

Hey @calvalini

You cannot use await outside an async function.
Give it a try and let me know :slight_smile:

Dani

Hey @Rob_McCourt

Make sure to be inside your truffle project folder.
Once there:
truffle develop
migrate --reset

Happy learning,
Dani

1 Like

@filip @dan-i any help with this issue? Im stucked for 2 or 3 days now

@javier_ortiz_mir

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

contract ("People", async function(){
  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")}));
  });
});

try:

let instance = await Peoplecontract.deployed();

cheers

1 Like

Hi @javier_ortiz_mir

Hi mate! I realised there is a typo bug in your contract.
let instance = await People.deployed();.
–> it should be let instance = await Peoplecontract.deployed() because you defined so in the constant above :slight_smile:

I hope it helps

1 Like

contract(“People”, async function() {
it(“should only work for Owner”, async function() {
let instance = await People.deployed();
await truffleAssert.fails(instance.deletePerson(accounts[2].address));
});
});

= Test should fail

Seems I actually need to create a person first, ditch the .address property on accounts[ ], add “accounts” as an argument to the first async function, and include a {from: } object.

contract(“People”, async function(accounts) {
it(“should only work for Owner”, async function() {
let instance = await People.deployed();
await instance.createPerson(“Bob”, 25, 200, {from: accounts[1], value: web3.utils.toWei(“1”, “ether”));
await truffleAsserts.fails(instance.deletePerson(accounts[1], {from: accounts[1]}));
});
});

Because it’s anyone other than accounts[0], the Owner, using deletePerson(), it should fail the test.

thats amazing. Can you go deeper into how that worked and what i did wrong? I cd in to the same folder before i said to you and got the error. But when you run truffle develop it just works then?

Cheers buddy

1 Like

Here’s my solution:

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

contract("People", async function(accounts){
it("shouldn't delete a person if it's not the owner", 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]}));
    });

    it("should delete a person if it's the owner", async function(){
        let instance = await People.deployed();
        await instance.deletePerson(accounts[0]);
        let result = await instance.getPerson();
        assert(result.name === "", "Person not deleted");
    });
});
1 Like

Truffle develop runs a local blockchain (it is ganache but without the visual tools). It is indeed the one that you will use once you get confident enough with coding Solidity :slight_smile:

Before migrating always make sure to have a blockchain running, and make sure to be into you project folder.

Happy coding :slight_smile:
Dani

1 Like

This is for the only deleted by owner assignment, the tests passed but not sure why I’d need to tests??

Let me know if I’m missing anything. Thanks.

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

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

  it("shouldn't create a person over age 150", async function(){
    let instance = await People.deployed();
    await truffleAssert.fails(
      instance.createPerson("Ricki", 175, 61,
          {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, 60, {value: 1000}),
      truffleAssert.ErrorType.REVERT);
  });
  it("should set senior status correctly", async function(){
    let instance = await People.deployed();
    await instance.createPerson("Ricki", 65, 61,
        {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 only allow the owner to delete a person", async function(){
    let instance = await People.deployed();
    await instance.createPerson("Ricki", 45, 61,
        {from: accounts[2], value: web3.utils.toWei("1", "ether")});
    await truffleAssert.fails(instance.deletePerson(accounts[2],
        {from: accounts[2]}), truffleAssert.ErrorType.REVERT);
  });
});

1 Like

Hey @keithra194

the tests passed but not sure why I’d need to tests??

Being able to test a project is crucial for a developer.
A test suite should be wrote for every project that you do and should be kept up to date and improved as much as possible.
Although you might not see the usefulness now you surely will when coding more complex projects.

Happy coding,
Dani

1 Like