Truffle Introduction

Hi @filip
When trying to install truffle (I am on a Mac) the terminal gave me “The operation was rejected by your operating system. npm ERR! It is likely you do not have the permissions to access this file as the current user”
I reinstalled as sudo and it worked OK. My question is: is it OK to install truffle as sudo?

1 Like

Hi @martintokyo
You can, but it’s better to set the correct permission on your global folder, see my previous message.
Otherwise all the package you will install with sudo will have the superAdmin user persmission (the user root)

1 Like

Thanks gabba for this rapid reply. I will do so.

1 Like

the threading on this forum needs work. It’s not clear who is answering who.

hey @filip or any other knowledgeable person!

I am stuck when trying to execute the test. I can’t see why it’s not working. I have checked my code and checked it against yours. I have tried it on two systems with almost identical errors:

TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException` cannot be used in the REPL
    at process.<anonymous> (repl.js:259:15)
    at process.emit (events.js:327:22)
    at process.emit (C:\Users\username\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\source-map-support\source-map-support.js:465:1)
    at processEmit [as emit] (C:\Users\username\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\signal-exit\index.js:155:1)
    at _addListener (events.js:358:14)
    at process.addListener (events.js:406:10)
    at Runner.run (C:\Users\username\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\runner.js:868:11)
    at Mocha.run (C:\Users\username\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\mocha.js:612:17)
    at C:\Users\username\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\test.js:128:1
    at new Promise (<anonymous>)
    at Object.run (C:\Users\username\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\test.js:127:1)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

I don’t think it’s the code, because it compiles and deploys correctly and functions and also, even when all the code in the test file is commented out, it still throws the same error :-S

Hi @Kiki

On which system did you try as i can see this one is Windows, what is the other one ?
Can you share your version of node and truffle ?

You can use the following answers to solve your REPL issue.

Or the one reinstall Node version 10.18.0 with nvm

https://forumtest.ivanontech.com/t/introduction-to-unit-testing/10052/45

1 Like

Hello @filip

I follow do everything exactly as you show in the video but then I get an error. I’m doing everything on Windows terminal

First I do this and it works fine:
let instance = await Helloworld.deployed()

But then I want to call the getMessage function:
instance.getMessage()

It gives the following error:
Uncaught TypeError: instance.getMessage is not a function

Although I checked in the instance and it does contain the getMessage function as is shown in the video.

Could you please help me solve this issue?

Hi @Cemil

Can you share your migration file ?
Use the preformatted tag to display your code correctly
thx you

1 Like

Hi @gabba

Here is my 1_initial_migration.js file

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

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

And this is the 2_Helloworld_deploy.js file

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

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

Many thanks,
Cemil

Hi @Cemil
Your migration files are correct, i guess

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

Is also included in you test.js ?

Can you try this command

truffle migrate --reset 

And try again ?

1 Like

Hi @gabba

It worked, thank you!

Cemil

OK, so I tried those steps, I had to mess around a bit with some permissions, but it’s working, thank you :slight_smile:

1 Like

Hi, anyone knows how to solve the problem here:


It worked perfectly well yesterday, but today I have got the error.

I have already tried the solution from this link, but it does not seem to work:
https://forumtest.ivanontech.com/t/introduction-to-unit-testing/10052/42

I have node version 10.18.0, Truffle 0.5.16, Web3.js 1.2.1.
Any help would be appreciated!

Hi @Golden_Pig_Coin
Can you show us your code ?
Where is your variable dave defined ?

1 Like

Hi @gabba
here is the code, it is from the People Project lectures. I want to create person directly in truffle console, deployed the contract without problem, then I used:
let instance = await People.delpoyed()
instance
instance.createPerson(dave, 180, 55)
just realized that I put age and height in wrong positions, but that should return error, not “dave is not defined” ?

contract People is Ownable{

    struct Person {
      uint id;
      string name;
      uint age;
      uint height;
      bool senior;
    }

    event personCreated(string name, bool senior);
    event personDeleted(string name, bool senior, address deletedBy);

    uint public balance;

    modifier costs(uint cost){
        require(msg.value >= cost);
        _;
    }

    mapping (address => Person) private people;
    address[] private creators;

    function createPerson(string memory name, uint age, uint height) public payable costs(1 ether){
      require(age < 150, "Age needs to be below 150");
      require(msg.value >= 1 ether);
      balance += msg.value;

        //This creates a person
        Person memory newPerson;
        newPerson.name = name;
        newPerson.age = age;
        newPerson.height = height;

        if(age >= 65){
           newPerson.senior = true;
       }
       else{
           newPerson.senior = false;
       }

        insertPerson(newPerson);
        creators.push(msg.sender);

        assert(
            keccak256(
                abi.encodePacked(
                    people[msg.sender].name,
                    people[msg.sender].age,
                    people[msg.sender].height,
                    people[msg.sender].senior
                )
            )
            ==
            keccak256(
                abi.encodePacked(
                    newPerson.name,
                    newPerson.age,
                    newPerson.height,
                    newPerson.senior
                )
            )
        );
        emit personCreated(newPerson.name, newPerson.senior);
    }
    function insertPerson(Person memory newPerson) private {
        address creator = msg.sender;
        people[creator] = newPerson;
    }
    function getPerson() public view returns(string memory name, uint age, uint height, bool senior){
        address creator = msg.sender;
        return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
    }
}

Hi @Golden_Pig_Coin
Your bug is in your test file not in your contract can you share your test file ?

1 Like

Hi @gabba, here is the test file:

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("shouldn't create a person with age over 150 years", async function(){

      await truffleAssert.fails(instance.createPerson("Bob", 200, 190, {value: web3.utils.toWei("1", "ether")}), truffleAssert.ErrorType.REVERT);
    });
    it("shouldn't create a person without payment", async function(){

      await truffleAssert.fails(instance.createPerson("Bob", 50, 190, {value: 1000}), truffleAssert.ErrorType.REVERT);
    });
    it("should set senior status correctly", async function(){

      await instance.createPerson("Bob", 65, 190, {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 result = await instance.getPerson();
      assert(result.age == 65, "Age not set correctly");
    });
    it("should not allow other account except the owner to delete people", async function(){
  
      await instance.createPerson("Lisa", 35, 170, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
      await truffleAssert.fails(instance.deletePerson(accounts[1], {from: accounts[1], value: web3.utils.toWei("1", "ether")}), truffleAssert.ErrorType.REVERT);
    }); 
    it("should only allow owner to delete people", async function(){
 
      await truffleAssert.passes(instance.deletePerson(accounts[1], {from: accounts[0]}));
    });

    it("should decrease owner's balance after adding people", async function(){
      let instance = await People.new();
      let initialBalance = parseInt(await web3.eth.getBalance(accounts[0]));
      await instance.createPerson("David", 38, 175,{from: accounts[0], value: web3.utils.toWei("1", "ether")});
      let newBalance = parseInt(await web3.eth.getBalance(accounts[0]));
      assert(newBalance < initialBalance, "owner's balance did not decrease");
    });

    it("should not be able to withdraw the contract fund except the owner", async function(){

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

    it("should only allow the contract owner to withdraw the contract funds", async function(){

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

});

Hi again @Golden_Pig_Coin ^^

Ohhh my bad i didn’t read well, you try to do it in the truffle console…
You need to do

instance.createPerson("dave", 200, 190)

with double quotes, or define dave as a variable

let dave = "dave";
instance.createPerson(dave, 200, 190)
2 Likes

Hi @gabba
thank you very much!
That explains why it worked on the previous day! I need to screenshot everything that I did correctly from now on!

In his example @filip codes:

assert(result.senior === true, "senior level not set");

I cannot see why he is using === rather than == given that senior is of type bool. I used == and it worked as intended.

Am I missing something?