Introduction to Unit Testing

Thanks for all of your help @dani69654!

I initiated the truffle setup into the Helloworld folder to follow along with the other lessons until this unit testing section required deleting those lines 867-889 etc in the uncaught error file and after that then this error.

I’ve updated Repo to include all folders and files that was a part of the truffle init inside the Helloworld directory/folder

Thanks again!

Hey @NeoKanoPheus

Could you please send me the link / the title of the lesson where is required to delete those lines?
Please try to create a brand new truffle project (truffle init), then copy and paste there your contracts and retry testing my truffle develop > migrate --reset > test.
If uncaught error file appears please send me a screenshot of your whole PowerShell window and I will have a look.

Happy coding,
Dani

2 Likes

Hi everyone,
I am having issue with this exercise,
I’ve read again and again my code to be sure it matches the course and my second test doesn’t pass.
Here is my contract:

Here’s the migration file:

Here’s my test
image
and results

If someone cn help It be very appreciated
Regards Guillaume

I tried to remove the payable part of the setMessage() function. It seems to have solved the issue.

Reviewed the whole chapter needed a refresh :sweat_smile:

Hey @guillaume_M-D

Schermata 2020-09-22 alle 10.16.57

You are not sending ether to the function setMessage try
await instance.setMessage('Testing Message',{value:1000000000});

You are correctly doing it in your migration file.

Let me know :slight_smile:

Happy learning,
Dani

1 Like

aah! Thanks a lot
I did not try without the account property. :ok_hand:
can I conclude that for tests, I can omit the sender address and the test will pass? It feels a bit risky. :thinking: :slight_smile:

Hi @guillaume_M-D

If you omit the sender account, it will automatically use accounts[0]

Cheers,
Dani

1 Like

Delete and reinstalling truffle config didn’t work but I just switched the solidity version in the code to 0.5.8 instead of 0.5.12 that the course is taught in and that worked in being abled to deploy Helloworld Contract…

However, in working on the Dapp Template for preparation for the Project Phase and assignment, I am getting this error messsage in the browser console, any idea how I can handle this or why it is throwing this error?

Uncaught TypeError: contractInstance.methods.createPerson is not a function
at HTMLButtonElement.inputData (main.js:17)
at HTMLButtonElement.dispatch (jquery-3.4.1.min.js:2)
at HTMLButtonElement.v.handle (jquery-3.4.1.min.js:2)
pageHook.js:42704 [Sep 23, 07:40:28 am] [pageHook]: [INFO]: TronLink initiated
inpage.js:1 MetaMask: MetaMask will soon stop reloading pages on network change.
For more information, see: https://docs.metamask.io/guide/ethereum-provider.html#ethereum-autorefreshonnetworkchange
Set ‘ethereum.autoRefreshOnNetworkChange’ to ‘false’ to silence this warning.
(anonymous) @ inpage.js:1
11main.js:17 Uncaught TypeError: contractInstance.methods.createPerson is not a function
at HTMLButtonElement.inputData (main.js:17)
at HTMLButtonElement.dispatch (jquery-3.4.1.min.js:2)
at HTMLButtonElement.v.handle (jquery-3.4.1.min.js:2)


var web3 = new Web3(Web3.givenProvider);
var contractInstance;

$(document).ready(function() {
    window.ethereum.enable().then(function(accounts){
      contractInstance = new web3.eth.Contract(window.abi, "0x0f2ACEbB46651f6e8161d78897DF1b51CD1F3Ff9", {from: accounts[0]});
    });
    $("#get_data_button").click(fetchAndDisplay);
    $("#add_data_button").click(inputData);

});

function inputData(){
  var name = $("#name_input").val();
  var age = $("#age_input").val();
  var height = $("#height_input").val();
  contractInstance.methods.createPerson(name, age, height).send({value: web3.utils.toWei("1", "ether")})
    .on('transactionHash', function(hash){
      console.log("tx hash");
    })
    .on('confirmation', function(confirmationNumber, receipt){
        console.log("conf");
    })
    .on('receipt', function(receipt){
      console.log(receipt);
    })
  }
function fetchAndDisplay(){
  contractInstance.methods.getPerson().call().then(function(res){
    displayInfo(res);
  });
}
function displayInfo(res){

  $("#name_output").text(res["name"]);
  $("#age_output").text(res["age"]);
  $("#height_output").text(res["height"]);
}

Hey @NeoKanoPheus

I edited your post to make the code readable.
Please always format you code by clicking the button:

image

What your console is telling is that contractInstance.methods.createPerson is not a function

You can try to console.log(contractInstance) and check if there is a function createPerson().

I would need to check the abi file you created, but before doing that, fix this one:
contractInstance = new web3.eth.Contract(window.abi, "0x0f2ACEbB46651f6e8161d78897DF1b51CD1F3Ff9", {from: accounts[0]});
You should remove window from window.abi, as you need to import a file called just abi.

The correct one should be: contractInstance = new web3.eth.Contract(abi, "0x0f2ACEbB46651f6e8161d78897DF1b51CD1F3Ff9", {from: accounts[0]});

If that does not work, post the whole abi file too.

Cheers,
Dani

1 Like

I really appreciate all of your help. Man. Thanks.

Here is the abi:

var abi = [
    {
      "constant": true,
      "inputs": [],
      "name": "getMessage",
      "outputs": [
        {
          "internalType": "string",
          "name": "",
          "type": "string"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "internalType": "string",
          "name": "newMessage",
          "type": "string"
        }
      ],
      "name": "setMessage",
      "outputs": [],
      "payable": true,
      "stateMutability": "payable",
      "type": "function"
    }
  ]
1 Like

Hey @NeoKanoPheus

In your ABI I do not see the function createPerson()

I see only getMessage() and setMessage()

Take the correct ABI and try again :slight_smile:

You can find the abi into your project folder > build > contracts

Keep me updated
Dani

1 Like

Duly noted.
Thank you very much

1 Like

How do I downgrade the node?

Same problems on linux

solved it by installing NVM and changed NODE.JS V12.18.1 ==> V10.18

then it worked

Hey @elterremoto

Check this FAQ: FAQ - How to downgrade Node.Js

Cheers,
Dani

2 Likes

Hello,

I had error ERR_INVALID_REPL_INPUT after running test

so I followed these instructions.

After that I have a new error:

Compiling your contracts...
===========================
Error: TypeError: Error parsing C:/Users/enrico/Desktop/coding/truffle/helloworld/contracts/Migrations.sol: Cannot destructure property 'body' of 'undefined' as it is undefined.
    at Object.compile (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\workflow-compile\legacy\index.js:80:1)

The Helloworld contract was not modified (before it was migrating it normally).

Do you have any idea how to fix this? thanks

@filip

Trying to run “test” in truffle console. Getting this message:

TypeError [ERR_INVALID_REPL_INPUT]: Listeners for uncaughtException cannot be used in the REPL

Any advice?

I found this thread which I think is about this issue, but not sure what to do: https://github.com/trufflesuite/truffle/issues/2463

1 Like

Hey @enrico

Check this FAQ: FAQ - How to downgrade Node.Js

Let me know if that fixes the issue.

Cheers,
Dani

1 Like

Slavenr’s solution worked for me!

1 Like