HoloWorld Tutorial: Writing Tests

Make sure that you've completed the last tutorial step before starting this one, writing functions.

Recall that in the last tutorial we added functions holoTextWrite and holoTextRead to our readerWriter.js file.

Now we wish to test those functions, and make sure that they do what we expect them to do. 

First we will add a test which will test holoTextWrite, then we'll do the other one. 

Once we've added tests we'll use the command line tools to run them, and see what happens. 

 

Setting up the test folder

In your text editor, open the HoloWorld application folder. There should be a folder called test. 

In order to get a stronger understanding of what to do, we will just delete all the sample code that is in there. Delete the sub-folder, as well as the sample.json file.

Now, create a new file in the test folder called readerWriter.json. We use the same name as the Zome that we are testing, by convention. 

Add the following code into it:

{
  "Tests": []
}

In between the square brackets, we will add a list of tests which will get executed by the test-runner in order. 

There are lots of advanced ways to test, but we will do the most basic. 

 

Testing holoTextWrite

Modify readerWriter.json so that it looks like this:

{
    "Tests": [
        {
            "Convey": "",
            "Zome": "",
            "FnName": "",
            "Input": "",
            "Output": ""
        }
    ]
}

We will fill in each property one by one. 

The "Convey" property should simply be a descriptive string of words, of what the test should do.

Set "Convey" to "write a holoText entry".

"Zome" should specify the Zome name. 

Set "Zome" to "readerWriter".

"FnName" needs to be the function that the test-runner should call. 

Set "FnName" to "holoTextWrite".

Now it gets more interesting...

What should the inputs and outputs of our function call be?

Let's write "Hello World" as an entry!

Set "Input" to "Hello World".

What should output be? Recall that we returned the hash of the entry we just created, but we don't know what that hash will be. Holochain provides replacement strings for these types of situations. 

"%h%" means use the hash of the latest entry into the local source chain!

Set "Output" to "%h%".

That's it. Now we can run our first test. 

 

Running Tests

Open up your command line terminal, and change directories so that you are in your main app folder, the one which contains 'dna' and 'test' sub-folders.

From here, we can use a Holochain command to run our tests. Type the following and hit enter:

$ hcdev test

You should see the following:

20337 original

We got our first test passing! If it didn't pass, check your spelling and syntax again.

 

Testing holoTextRead

Testing holoTextRead is similar to testing holoTextWrite. We just need to change a few things. 

Modify readerWriter.json so it looks like this:

{
    "Tests": [
        {
            "Convey": "write a holoText entry",
            "Zome": "readerWriter",
            "FnName": "holoTextWrite",
            "Input": "Hello World",
            "Output": "%h%"
        },
        {
            "Convey": "",
            "Zome": "",
            "FnName": "",
            "Input": "",
            "Output": ""
        }
    ]
}

Fill in the following properties with the following values: 

  • "Convey": "read a holoText entry"
  • "Zome": "readerWriter"
  • "FnName": "holoTextRead"

Can you guess what the value of "Input" could be? Remember that the tests are running in order.

We can use the same replacements strings for inputs, as for outputs, so we can use "%h%" again, to reference the latest hash in the local source chain. Since we just wrote the entry successfully, we know it will be there, so the output should the "Hello World"!

Fill in the following properties with the following values: 

  • "Input": "%h%"
  • "Output": "Hello World"

Run the following command from your terminal again:

$ hcdev test

You should get a result like this:

20348 original

 

Conclusion

In the end, our readerWriter.json test file should look like this:

{
    "Tests": [
        {
            "Convey": "write a holoText entry",
            "Zome": "readerWriter",
            "FnName": "holoTextWrite",
            "Input": "Hello World",
            "Output": "%h%"
        },
        {
            "Convey": "read a holoText entry",
            "Zome": "readerWriter",
            "FnName": "holoTextRead",
            "Input": "%h%",
            "Output": "Hello World"
        }
    ]
}

Now that we've written tests, we will expand the functionality of our application to include sharing our holoTextentries out to the DHT.

Go back to the Writing Tests page, or  continue to the next tutorial.