Introduction

In the first part of this series, we wrote about how to setup the environment for unit testing the Kin SDKs, using testrpc and Truffle. In this post, I’ll describe how to integrate it with Xcode, to make the mock blockchain available when running the tests.

Integrating testrpc within Xcode tests

Once testrpc is set up, it needs to be started before tests are launched. To achieve this, we need to trigger a few actions before and after tests launch by editing our scheme in Xcode. All tests are run in a`KinTestHost` app, another target in the `KinSDK` project (and as the name suggests, it’s the host app for tests).

Xcode targets: the SDK itself, tests, and the test host app

In the scheme selection dropdown, we will edit the KinTestHost scheme:

Scheme selection, to configure pre and post actions

Then, under the Test section, we select Pre-actions:

Adding a Pre-action script to the tests host app

This is a short script that basically triggers the make command prepare-tests, that will be responsible for starting testrpc and Truffle and making it listen to localhost and the desired port (8545 by default). All of this is done via Makefile ( make prepare-tests ) and the scripts in the truffle/scripts folder mentioned earlier.

This way, in our test classes we initialize our KinClient using the local, mock ethereum node:

Initializing KinClient to use the local node, when testing

Finally, after our tests have run (and are hopefully successful!), we should kill testrpc. Again, we open the Xcode scheme, but this time we run a script as a Post-action:

Adding a Post-action script to the tests host app

And again, we use our Makefile and associated scripts to shut down testrpc.

Giving Back to OSS and Making Tests Succeed

Unfortunately, existing frameworks are not always a bed of roses. We bumped into a hurdle when writing the test for sending transactions. Sending a transaction with Ethereum accounts requires first checking the count of currently pending transactions (the nonce parameter), so each transaction is unique. This is done by calling the method getTransactionCount in the node. As defined in the Ethereum Wiki, when sending numbers in responses — like the number of pending transactions — they should be encoded as hexadecimal, prefixed by 0x , and in the most compact representation.

However, testrpc had a bug: If getTransactionCount would return 6, for example, it would be encoded as 0x06 instead of 0x6 , the correct way. This would lead to a crash in the Geth framework used in the SDK, that in turn would cause tests to fail.

After a few hours of debugging, I managed to find the two sources of this bug, and fix it. A pull request was opened (and merged a few weeks later).

We can now finally run the tests and…

Finally!

Wrap up

This was a great opportunity to learn more and understand better concepts in different areas: the Ethereum blockchain and its internals, using a mock node to mock it, GNU Make, and pre/post actions in Xcode schemes. The feeling of contributing to open source projects (in this case, to the Truffle framework) is very satisfying. Also, having the CI and tests setup will make contributing to the SDKs safer (by detecting possible errors earlier), helping the Kin community to grow and make Kin succeed. I’m excited for what’s coming next!

As a reminder, the Kin SDKs are open source, and in our GitHub you can find the iOS SDK (which includes the project described in this post) as well as the Android SDK. Stay tuned for the 3rd and final part of this series. If you have any comments or questions, feel free to comment here, or ping me on Twitter @natanrolnik.