Introduction to Unit Testing

Nevermind.
Downgrading to Node v10.18.0 worked.

1 Like

@Flaflaflohi
Thanks for reaching out!
Try using node version 10 or below.

To check version
npm -v

Need help with the test:

it("should delete the person ", async function(){
    let instance = await People.deployed();
    await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
    await assert(instance.deletePerson({from: accounts[0]}));
  });

And the error I get is:
1) Contract: People
should delete the person :
TypeError: Cannot read property ‘0’ of undefined
at Context. (test\peopletest.js:33:55)
at process._tickCallback (internal/process/next_tick.js:68:7)

Where do I have a mistake?

@rostyslavdzhohola
The error suggest that accounts array is undefined. That means your metamask account is not connected to the dApp while calling the account during the test.

window.ethereum.enable().then(function(accounts){
     await assert(instance.deletePerson({from: accounts[0]}));
});

The above enables account and defines accounts.

1 Like

So, I have tried to fix the REPL error in Node v 12 with the following:

npm un -g truffle
npm i -g truffle@nodeLTS

However, I now get the following error when trying to get to the truffle console:

> Something went wrong while attempting to connect to the network. Check your network configuration.

Could not connect to your Ethereum client with the following parameters:
    - host       > 127.0.0.1
    - port       > 7545
    - network_id > 5777
Please check that your Ethereum client:
    - is running
    - is accepting RPC connections (i.e., "--rpc" option is used in geth)
    - is accessible over the network
    - is properly configured in your Truffle configuration file (truffle-config.js)

Truffle v5.1.14-nodeLTS.0 (core: 5.1.13)
Node v12.18.3

I assume that I now have to re configure my truffle-config.js file based on the new version. Does anyone know how this can be fixed?

Hey @Flaflaflohi

Some tips before you continue:

  1. downgrade your node, we already saw too many times that v12 + gives errors. Please install this one HERE

  2. From what you’ve posted above, seems like truffle cannot connect to the local blockchain.
    Open your terminal then

truffle develop
1 Like

This fixed my problem exactly TYSM!

Thanks!
By the way, if you’re in mac, use command+shift+dot to see hidden files.
Then look for the runner.js file at:

usr/local/lib/node_modules/mocha/lib/

or just look for it from the finder.

Hi community,
I encounter a problem when I try to test my contract. I do not know what is the problem. Help me solve thx!!




Hello sir, truffle console said that function setMessage(string memory newMessage) parameter is not being used in the function body. Please check on your HelloWorld.sol, code line 12 said message = message, you have a bad declaration on your variables.

Hope you find this useful.
If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

HI @thecil,
The solution that you provide isn’t working for me.




Hey @Marcus_Tang

Which version of node are you using?
Proceed as follow:

  • Open your Power Shell;
  • Type: node -v;
  • The Power Shell shows you the version of your nodeJs;

Schermata 2020-09-09 alle 10.32.28

If the version is 12 or higher, please downgrade it to this one: https://nodejs.org/dist/v11.12.0/

2 Likes

Hi @dan-i
the version that i am using is the same as yours.

Hey @Marcus_Tang,

Yup mine was an example to show how to do node -v, please proceed downgrading it to this one: https://nodejs.org/dist/v11.12.0/

2 Likes

Yo @dan-i
Alright but I dont quite undertand anything in that link…Please guide me thx

Hey @Marcus_Tang, you can proceed as follow:

  • Remove node from your computer : open control panel > select node and click uninstall.

  • Verify that node has been uninstalled by typing node -v you should see something like this:

  • Open the link I sent before, you should download the .msi file (based on your computer). Try first to download and install node-v11.12.0-x64.msi , if that does not open download and install node-v11.12.0-x86.msi

  • Once done, type again node -v, you should now have the version 11.12.0
    Schermata 2020-09-09 alle 12.53.39

  • Test now Truffle and check if it works.

Keep me posted,
Dani

1 Like

Did this but now getting Test.run is not a function error message.

Here’s my code (same as the course code but self typed):

const Helloworld = artifacts.require(“Helloworld”);
contract (“HelloWorld”, async function() {
it(“should initialize correctly”, async function() {
let instance = await Helloworld.deployed();
let message = await instance.getMessage();
assert(message ===“Helloooo Againnnn Eboooo…”);
});
});

Thanks.!

Hey @NeoKanoPheus

Open your poweshell and type:

truffle --version
node --version

Let me know both of them please.
Also I would ask you to upload your code in github, I want to see if I can replicate the issue.

2 Likes

truffle v = 5.0.42
node v = 12.18.3

Thank you!

Hey @NeoKanoPheus

I do see that your repository does not contain a Truffle setup.
Make sure to navigate into a folder then type truffle init
This will intialize your folder with the setup necessary for Truffle to work correctly.

If you are doing that already please update your repo :slight_smile:

Let me know,
Dani

2 Likes