If you want to follow along, make sure you've followed the instructions for getting setup.
The Counter contract
This example uses a simple counter contract. Here's the Clarity code:
Unit test setup
At the top of your unit testing file, you'll need to import a few things to get started:
Calling read-only functions
Use the rov
function, which stands for "read-only-value", to call a read-only function and only get the value returned. For example:
If your read-only function returns a response
type, use rovOk
and rovErr
to automatically check the response and return either the ok
or err
value.
Calling public functions
Use the txOk
function to call a public function and check that it returns an ok
. For example:
You can also use tx
to get the full response value, whether the function is ok
or err
.
Or, you can use txErr
to expect an err
type. This is helpful for testing your function's validation logic.
In the increment
function, an error is returned if the step
is greater than 5. You can test this like so:
Getting variables and map entries
Use varGet
and mapGet
to get the value of a variable or map entry.
Handling transaction events
Use filterEvents
to get the events emitted by a transaction. This is useful for testing that your contract emits the correct events.