Introduction to Unit Testing

I got nodist from this source-forge site:

It’s important, though, that you combine it with the command

$ npm rebuild

Without that command, I always get an error message when I try to ‘migrate’ my contracts.

I had the same problem a lot of people had. This is the solution that worked for me! Thank you guys!

3 Likes

hi ivga and Paultier,

I was able to solved this error. This is what I did.

  1. locate the runner.js file - it’s in AppData under your user name for PC. Make sure to allow viewing on “hidden” files. (C:\Users\admin\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib)
  2. Once I located the file, I open it with code editor (Atom or Visual Studio).
  3. I follow the blog and in the runner file, search for code lines “process.on(‘uncaughtException’, uncaught);”. Once you found that then delete that line (see attached snippit)
  4. i followed the blog on “same repro steps”. I exit truffle console by hitting “Ctrl + D”, then I enter “npx truffle unbox metacoin”, then entering “npx truffle dev”, then enter “test”. Or after entering “npx truffle unbox metacoin”, you can go back in the console and enter “test”, it will now work. See repro steps in attached snippet

Hope this helps

4 Likes

This worked perfectly for me! Thanks!

2 Likes

Hello, I’ve followed the instructions people suggested in the forum. This includes, but is not limited to:

  • Deleting the lines of code from Runner.js
  • Uninstalling and re-installing Truffle
  • Removing build and re-migrating

Ultimately it has lead to this:

So the code kinda works - Ganache sees everything working fine - but I don’t get those error/test messages like in the video. Unless this is ok? I’m not sure. I’m pretty sure my code is ok. Here’s the Helloworld test code:

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();
    //Use 3 equal signs for comparison of strings
    assert(message === "Hello Again!"
  , "Message should be Hello Again!");
  });
  it("should set the message correctly", async function(){
    let instance = await Helloworld.deployed();
    await instance.setMessage("Testing Message");
    assert(message === "Testing Message"
  , "Message should be the same as we set it to");
  });
});

I would move on to future lectures, but I really want to know why it’s not working, and I don’t want to compound on errors!

1 Like

@Uyan
Apologies for late reply.
Yes, the js code is fine :slight_smile:

The code executes fine for me as well.

I will need more info to reproduce the error. (that is not getting the messages).
Can you try directly from the GitHub code provided in the tutorial and see whether its working or not? I mean, are you getting test notifications or not?

Thanks

1 Like

I’ve figured out the problem, and I’ll post how I figured it out here for future reference. Basically I was a bit silly!

I found a similar problem through an internet search. You can see their problem here:

Link

https://courses.blockgeeks.com/question/cannot-find-artifacts-error-when-running-test/

I tried their solution of running specifically

truffle test ./test/Peopletest.js
rather than just
truffle test
and this is what I got:

Pic 1

It can’t find the test function. Well, this is where I was silly:

Pic 2

I thought the test function, along with the other files pictured, was/were already in the test folder. But they weren’t! After putting the test function in the correct folder:

Pic 3

The code works perfectly. Back to work!

1 Like

@Uyan
Great to see you to solve.
Next time remember to save it :slight_smile:

1 Like

While testing I am running into the issue of my address[0] running out of ETH. When migrating/deploying my contract I send 10 ETH to it. As I’m running my tests, my account eventually runs out of ETH, at which point I just restart truffle and reconnect the project.

Is there are more elegant way of handling this?

For testing smart contracts, does anyone know how to stub a smart contract function? For example, in the coin flip smart contract, how can we force the random() function to return a particular value in our mocha tests?

@Daveed
I suppose you are using Ganache, where we get 100 ETH per account.
So what is the reason your account runs out of ETH so quickly?

1 Like

I did run into the same issue as so many regarding the uncaughtException.
Interestingly enough I was not able to locate the lines “process.on(‘uncaughtException’…” in the runner.js file.
As I am working on a Win10 Machine I endedd up using NVM and installing node.js version 10.22.0
NVM: https://github.com/coreybutler/nvm-windows/releases
NVM instructions: https://github.com/coreybutler/nvm-windows

This solved the issue in my case.

1 Like

@rkindle
Thanks for reaching out!

Great to see your resolve the issue :+1:

1 Like

@Daveed
The reason we are using RANDOM function is to generate random value hence returning particular value is not possible.

1 Like

Yes, I understand that. I’m wanting to mock the random function for testing purposes so I can test the execution path when the user wins, and test the execution path when they lose.

The main question I have here is, “how do I mock smart contract functions for testing purposes?”

As I mentioned in my previous message, each time I deploy the contract I send it 10 ETH (https://github.com/TrustyTexan/SimpleCoinFlipDapp/blob/master/migrations/2_deploy_contracts.js#L5).

Thus, after 10 deploys the account[0] has been depleted. Is there a way I can refill the account? Can I switch to using a different account. There must be an elegant way of handling this without needing to change the amount of ETH I send during the deployment.

@Daveed
We cannot refill ETH in account in Ganache but you can restart the ganache blockchain account with a higher default balance.

You just need to change ACCOUNT DEFAULT BALANCE value from 100 to 10,000 or which ever number you feel right according to your dApp.

Happy coding :slight_smile:

1 Like

Mocking a contract essentially means creating a second version of that contract which behaves very similar to the original one, but in a way that can be easily controlled by the developer. You often end up with complex contracts where you only want to unit-test small parts of the contract. The problem is what if testing this small part requires a very specific contract state that is difficult to end up in?

You could write complex test setup logic everytime that brings in the contract in the required state or you write a mock. Mocking a contract is easy with inheritance. Simply create a second mock contract that inherits from the original one. Now you can override functions to your mock.

In your case, you can use the mock function to test the execution path whether the user wins or not.
example: https://dappsdev.org/hands-on/testing/solidity-mocks/

1 Like

Your solution works fine for me.
After changing the runner.js file, I had to exit my truffle console ( .exit ) and restart the truffle console .again.

1 Like

I need help. I have tried different solutions offered in here and on the internet but I get the same error. I don’t know what I am doing here. Please advice.

truffle(ganache)> test
Using network 'ganache'.


Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



   ⠸ Saving migration to chain.Success


TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException` cannot be used in the REPL
    at process.<anonymous> (repl.js:256:15)
    at process.emit (events.js:327:22)
    at process.emit (C:\Users\Rostyslav\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\source-map-support\source-map-support.js:465:1)
    at processEmit [as emit] (C:\Users\Rostyslav\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\Rostyslav\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\runner.js:868:11)
    at Mocha.run (C:\Users\Rostyslav\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\mocha.js:612:17)
    at C:\Users\Rostyslav\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\test.js:128:1
    at new Promise (<anonymous>)
    at Object.run (C:\Users\Rostyslav\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\test.js:127:1)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)