Image for post
Image for post

Testing and debugging a Convector Smart Contract

Walter Montes
Mar 21, 2019 · 5 min read

While developing a smart contract, it’s wise to avoid time-wasting tasks. One is the need to deploy in a heavy to setup environment just to test your changes.

With Convector, you can run local unit tests with a Mock ledger that you can also use to debug your code. With the latest versions of Hurley and Convector Smart Contracts it’s now even possible to debug straight from the blockchain.

Unit tests

In case you haven’t heard of them, it’s a way to create some code that automatically tests your code (yeah, trust the machines 😁).

In general, you grab a unit of code that makes sense to test out (a module, a function, a procedure, a group of functions) and code a function that calls that and checks results. These tests can be run while compiling, integrating, or deploying your code, so that you can be sure that what you expected to happen still happens.

  • Fast and agile development. Making refactors or even small changes to parts of your code that already worked can definitely be worrisome. With Unite Tests if you change something, you can run the unit tests to see if the software still behaves the way you want it to.
  • Thinking on tests before coding can make you build better-thought code, you can think of the inputs and behavior before jumping into code. Unit tests are basic units on BDD (and TDD). Your blockchain components should not break a good engineering process or feel alien to your day-to-day.
  • Find bugs early and quickly! Even before going to integration.
  • Easier integration of multiple developers. Since the “contract” of the functions are reflected in the tests, if somebody breaks it, it’s easier to identify dependencies changing, hence maintaining and changing the code becomes easier with multiple individuals turning coffee into code.
  • Easier documentation. Imagine you are new to the team, you want to better understand what a function is expecting and returning. With a unit test, you can pick up an idea of what is happening faster than reading the code and going through dependencies on multiple layers.
  • Debug, debug, debug. Instead of having to run that new piece of code in a complex and slow to start full environment, you can test if your business logic works right where you are building it.

Overall Unit Tests can save you a lot of time and money, while will also help you maintain a healthy code base.

How to do it

Do you have Convector-CLI installed yet? Be sure to have Node 8.11.0 and Docker Community Edition.

Make sure you have Convector-CLI version 1.1.0

$ npm i -g @worldsibu/convector-cli

Now, let’s start by creating a new Convector project:

$ conv new bughunting -c mybuggychaincode$ cd bughunting$ npm install

By default, a test file was created for your chaincode inside of ./packages/mybuggychaincode/tests/mybuggychaincode.spec.ts

We use mocha for tests, which is a renown lib for that. That file is just bringing the plumbing and testing the default create function that the controller has.

Go ahead and run it! In your terminal run npm run test

Image for post
Image for post
Expected test result

That’s it! You just ran your first unit test for a Convector smart contract. That is also what would be run automatically in your CI/CD process.

Mocha is quite extensive, you can check all the options here. For now, let’s create an extra function and change the smart contract a little bit.

Update the following files mybuggychaincode.controller.ts , mybuggychaincode.model.ts , and mybuggychaincode.spec.ts accordingly:

Go ahead and run the test:

$ npm test

The 4 testing functions should have run successfully!


Debugging is another useful task, with the Convector Suite you can debug your unit tests but you can also debug your code running in the blockchain (😲 yeah!).

Go to ./packages/mybuggychaincode-cc/package.json and replace the contents of the test task with the following.

"test": "npm run build && mocha --inspect --debug=8888 -r ts-node/register tests/*.spec.ts --reporter spec"

The --inspect param will let you connect a debugger and walk through your code. I also changed the port to avoid collisions with other processes.

You can use your preferred debugger. I like to use Chrome DevTools for Node, so I just go to chrome://inspect in my browser and click on Open dedicated DevTools for Node .

Click on Add connection and enter localhost:8888 to listen for that port.

Image for post
Image for post

Go back to your code and in the test file located in ./packages/mybuggychaincode/tests/mybuggychaincode.spec.ts put a debugger; somewhere.

Image for post
Image for post
Debugging code.

Run npm test and voilá! You should now be able to debug nicely your code.

Image for post
Image for post

We use Hurley and since 1.0.0 it now supports a debug param.

You will need two new tasks in your CLI (if you don’t have them already). Go to your root package.json and look for cc:start:debug and cc:install:debug .

$ npm run cc:start:debug — mybuggychaincode

If you see this, it’s running ready for debugging.

Image for post
Image for post

Listen for the debugging port in 9990 .

Image for post
Image for post

All you need to do now is make a dummy call for the containers to be provisioned and the code loaded.

# It will throw an expected error - This will take some seconds while downloading dependencies again in the rest of the organizations
$ hurl invoke mybuggychaincode init

Now go to your DevTools and explore the source code.

You will see the native code running:

Image for post
Image for post


No longer operational

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store