Unit testing is crucial for ensuring that your dApp behaves as expected before deploying it. In CrabRolls, you can create comprehensive unit tests to validate your dApp’s functionality. This guide will walk you through the process of setting up and writing unit tests for your dApp.
Writing Your First Unit Test
Create a New Test Module:
Inside your application code, typically at the bottom of your source code or in a separate tests module, you can define your tests.
Define a Test Function:
Tests in CrabRolls are asynchronous. Use the async_std::test attribute to define an async test function.
Create an Application Instance:
Instantiate your application within the test function.
Create a Tester Instance:
Use the Tester utility to create a tester from your application instance. The tester helps you simulate and validate the application’s behavior.
By default the MockupOptions struct is used to configure the tester. You can customize the tester behavior by changing the options in the MockupOptions struct. The struct has the following fields:
portal_config: The configuration of the portal used by the tester, a enum of PortalHandlerConfig with the options:
This will be used to configure the behavior of the tester when handling deposits, more about this in the Wallet abstraction testing section.
Prepare Input Payload:
Next, prepare the input payload for the application based on the test scenario.
Simulate Application Advance:
Use the tester to advance the state of your application with the prepared payload.
You can also inspect the application state:
Validate Test Results:
Check the results of the advance operation. Validate the status, errors, and outputs to ensure the application behaves as expected.
The output of the advance and inspect operations is an object with the main fields to verify the application status, errors, and outputs. The Result object currently has the following fields:
status: The status of the application after the operation. You can get the status using the is_accepted, is_rejected to check if the application was accepted or rejected.
error: An optional error message if the application failed. You can check if the application failed using the is_errored method and get the error message using the get_error method.
outputs: A vector of outputs generated by the application. You can check the outputs vector using the get_outputs method and validate the outputs content.
metadata: A metadata object with additional information about the application advance (only available in the advance operation). To get the metadata object, use the get_metadata method. The metadata object has the following fields:
input_index: The index of the input that was processed.
sender: The address of the sender of the input.
block_number: The block number of the application.
timestamp: The timestamp of the input processing.
Complete Test Case Example
Here’s an example of a complete test case following the steps above for an echo application like the Echo example:
Running Tests
To run your tests, you can use the cargo test command. This command will compile your application and run all the tests defined in your application.
The output will show the results of each test, including the status and any errors encountered during the test execution.
Wallet abstraction testing
When working with deposits in CrabRolls, you can test various scenarios involving Ether, ERC20, ERC721, and ERC1155 tokens. Below is a guide on setting up and writing unit tests for deposit and withdrawal operations within your dApp, specifically focusing on these types of assets.
The following steps illustrate how to write and execute tests for handling deposits (Ether, ERC20, ERC721, and ERC1155) and their corresponding functions, is be used the WalletAbstractionApp as base application.
Make deposit
The Tester utility has a deposit method that allows you to simulate deposits of Ether, ERC20, ERC721, and ERC1155 tokens. The deposit method has the following signature:
The Deposit enum has the following variants:
Example usage on a test case:
Ether
Ether wallet abstraction has the following methods from the Tester utility:
Example usage on a test case:
ERC20
ERC20 wallet abstraction has the following methods from the Tester utility:
Example usage on a test case:
ERC721
ERC721 wallet abstraction has the following methods from the Tester utility:
Example usage on a test case:
ERC1155
ERC1155 wallet abstraction has the following methods from the Tester utility: