Introduction to Unit Testing

I was having the same issues as the rest of the forum with running test but it not working so I had to downgrade node.js.

When i use test, it runs now but it always gives me “0 passing”.

I’m not sure if this is problem with node or my code? I also saw there is a new Smart Contract 201 class. Should I just do that if this old version has compatibility issues?

Here is my code:

HelloWorld.js

pragma solidity 0.5.12;

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

  function getMessage() public view returns (string memory) {
    return message;
  }

  function setMessage(string memory _message) public payable {
    message = _message;
  }
}

2_HelloWorld_deploy.js

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

module.exports = function(deployer, network, accounts){
  deployer.deploy(HelloWorld).then(function(instance){
    instance.setMessage("PROMISE", {value: 100000000000000000, from: accounts[0]}).then(function(){
        console.log("SET SUCCESS");
      }).catch(function(err){
        console.log("ERROR: " + err);
      });
  }).catch(function(err){
    console.log("Deloy failed: " + err);
  });
};

HelloWorldTest.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 == "PROMISE");
  });
});

Thanks for the help!

Here is the terminal screenshot, not sure why I’m getting 0 passing when in the video it shows Filip gets 2 passing?

Hi @mervxxgotti

Your test runs fine in my computer.

Run:

  • truffle develop

Once the blockchain is ready

  • test

Regards,
Dani

1 Like

Tried running > truffle develop then > test

Still gets “0 passing”, is there something wrong with my assert function? or is this a version issue? even when I change the setMessage string so it doesn’t match the assert function and should fail, it still reports “0 passing” so “0 passing” shows regardless.

Even when I add in the code from the second part of the Unit Testing Introduction, I still get “0 passing” when I thought I should be getting “2 passing” and the it() messages?

Thanks for all the help!

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 === "PROMISE", "FAIL!");
  });
  it("should set the message correctly", async function(){
    let instance = await HelloWorld.deployed();
    await instance.setMessage("TESTTEST");
    let message = await instance.getMessage();
    assert(message === "TESTTEST", "FAIL2!");
  });
});

Hey @mervxxgotti

You see ‘0 passing’ because your tests are not running however I was able to copy/paste your code and run it.
There has to be something wrong somewhere else, push your code on GitHub and give me the link so that I can copy and test.

Thanks
Dani

It’s working now. While figuring out how to push to GitHub I noticed the test .js file was not actually in the test folder but I made it accidentally outside of it :sweat_smile:

Thanks so much for you patience!

Since it’s necessary anyway, does Ivan On Tech have any lessons on using GitHub or can you recommend me any good tutorials?

Thanks!

1 Like

I learned git with lots of practice and google :smiley:
It can seem annoying at the beginning but the more you practice, the more it will become clear.
For any question, YouTube is also full of Git tutorial.