Introduction to Unit Testing

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

@Karolis_Abramavicius Thank you so much… This was very easy to follow and helps a lot! :+1: :hugs:

1 Like

I had to downgrade node js from 12 to 10.16.0.

Here’s the link on how to change versions if your’e on a Mac.

https://stackoverflow.com/questions/47008159/how-to-downgrade-node-version

1 Like

Can anyone help me with this error when i run the test?

helloworld.sol

pragma solidity 0.5.12;

contract Helloworld {
  string message = "Hello World!";

  function getMessage() public view returns (string memory) {
    return message;
  }
  function setMessage(string memory newMessage) public payable {
  message = message;
  }
}

//Helloworld_Deploy.js

const Helloworld = artifacts.require("Helloworld");

module.exports = function(deployer, network, accounts){
  deployer.deploy(Helloworld).then(function(instance){
      instance.setMessage("Hello Again!", {value: 1000000, from: accounts[0]}).then(function(){
        console.log("Success");
      }).catch(function(err){
        console.log("error: " + err);
      });
  }).catch(function(err){
    console.log("Deploy failed " + err);
  });
};

//initial migration

const Migrations = artifacts.require("Migrations");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

//Helloworld.js

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 === "Hello Again!");
  });
});

Not sure if i can go further until i figure this error out.

Thanks chaps.

Rob.

1 Like

Hey @Rob_McCourt

You have a warning and an error:

warning:
The parameter string memory message is not used and can be removed.

error
This is related to the Node version installed.
Click this link and follow the guide to downgrade it:
https://forum.ivanontech.com/t/faq-how-to-downgrade-node-js/22908/2

let me know,
Dani

works perfectly thanks…

1 Like

Could somebody please tell me what’s going on here? I don’t understand the error. I’ve looked over and over again at the code from the video and I cannot figure out what’s wrong. Thank you.

What was the last command?

1 Like

Hey @mburtonshaw

From your screenshot it seems the tx failed due to a revert statement.
I am not able to provide more info as I need a bit of context.
Which function were you calling?
Also as @Rob_McCourt suggested, what was the last command you sent?

Cheers,
Dani

1 Like

@Rob_McCourt @dan-i
I entered “truffle console” and then “test”. I may have entered “migrate” before “test” but I don’t think I did

After running Test I keep receiving this error

1 Like

hey @kryptokrymmenos

Please follow this faq and downgrade node js: FAQ - How to downgrade Node.Js

cheers,
Dani

2 Likes

Would you please share your github repo? I would like to take a look.

thanks
Dani

1 Like

Sure

https://github.com/MBurtonshaw/practice/tree/main/newGuy/peopleProject

1 Like

Hey @mburtonshaw

The issue pops up also when deploying your contract.
You are trying to send ether to a non - payable function in your migration file.

instance.setMessage("Hello again", {value: 1000000, from: accounts[0]}).

Remove value:

instance.setMessage("Hello again", { from: accounts[0]}).

Cheers,
Dani

1 Like

Hi I would like to get help to understand a supply chain code that is about creating an item that costs a specific amount of wei and then track it via blockchain, here is the questions and the code:

1- What does this mean: ItemManager.SupplyChainSteps _step; why not, we create only : SupplyChainSteps -Step?

2-What does this command mean: ItemManager parentContract; as I understood that we created a variable called parentContract of type ItemManager, but what is the purpose of that and what does it mean a variable of type ItemManager?

3- Why did you use a constructor here: and for which reason?

constructor(ItemManager _parentContract, uint _priceInWei, uint _index) public {

priceInWei = _priceInWei;

index = _index;

parentContract = _parentContract;

}

4-what is the function of this command:

address(parentContract).call{value:msg.value}(abi.encodeWithSignature("triggerPayment(uint256)", index));

5-What is the meaning of : Item item = new Item(this, _priceInWei, index);

what does it mean: this  and what did we create in this case and why?



pragma solidity ^0.6.4;

contract Item {
 uint public priceInWei;
 uint public paidWei;
 uint public index;

 ItemManager parentContract;

 constructor(ItemManager _parentContract, uint _priceInWei, uint _index) public {
     
 priceInWei = _priceInWei;
 index = _index;
 
 }

 receive() external payable {
 require(msg.value == priceInWei, "We don't support partial payments");
 require(paidWei == 0, "Item is already paid!");
 paidWei += msg.value;
 (bool success, ) = address(parentContract).call{value:msg.value}(abi.encodeWithSignature("triggerPayment(uint256)", index));
 require(success, "Delivery did not work");

 }

 fallback () external {

 }

}


contract ItemManager {

 struct S_Item {
 Item _item;
 ItemManager.SupplyChainSteps _step;
 string _identifier;
 }
 mapping(uint => S_Item) public items;
 uint index;

 enum SupplyChainSteps {Created, Paid, Delivered}

 event SupplyChainStep(uint _itemIndex, uint _step, address _address);

 function createItem(string memory _identifier, uint _priceInWei) public {
 Item item = new Item(this, _priceInWei, index);
 items[index]._item = item;
 items[index]._step = SupplyChainSteps.Created;
 items[index]._identifier = _identifier;
 emit SupplyChainStep(index, uint(items[index]._step), address(item));
 index++;
 }

 function triggerPayment(uint _index) public payable {
 Item item = items[_index]._item;
 require(address(item) == msg.sender, "Only items are allowed to update themselves"
);
 require(item.priceInWei() == msg.value, "Not fully paid yet");
 require(items[index]._step == SupplyChainSteps.Created, "Item is further in the supply chain");
 items[_index]._step = SupplyChainSteps.Paid;
 emit SupplyChainStep(_index, uint(items[_index]._step), address(item));
 }

 function triggerDelivery(uint _index) public {
 require(items[_index]._step == SupplyChainSteps.Paid, "Item is further in the supply chain");
 items[_index]._step = SupplyChainSteps.Delivered;
 emit SupplyChainStep(_index, uint(items[_index]._step), address(items[_index]._item));
 }
}

I had exactly the same problem and I had to roll back to node 10. Since I have project that need node 12 and I have installed node on my user account (~/whereIinstalledIt) (not in /usr/local/bin/node) I created file links to point node, npm, npx, truffle to the version 10 or 12.

you can also use npm to switch install version see https://www.digitalocean.com/community/tutorials/nodejs-node-version-manager
for details

Hello
I have a problem with the code.
In the second part Building our First Test Part 2. The program does not work for me as intended. I don’t understand if the code is for payable or not payable functions

Works for me making not payable the function, but i cannot create a test that works for payable functions.
image

image

Hey @camarosan

The code does not work if you try to send eth to a non-payable function such as setMessage()

If you want to send eth to your function, it needs to be payable
function setMessage(string memory _msg) public payable {}