Test a Transfer Transaction
Testing each function is an important part of developing pallets for production. This guide steps you through best practices for writing test cases for a basic transfer
function.
Outline the transfer
function
transfer
functionA transfer function has two key elements: subtracting a balance from an account and adding that balance to another account. Here, we'll start by outlining this function:
Check that the sender has enough balance
The first thing to verify, is whether the sender has enough balance. In a separate tests.rs
file, write out this first test case:
Configure error handling
To implement some error check, replace mutate
with try_mutate
to use ensure!
. This will check whether bal is greater or equal to amount and throw an error message if not:
Run cargo test
from your pallet's directory.
Check that sending account doesn't go below minimum balance
Inside your transfer_works
function:
Check that both tests work together
Use #[transactional]
to generate a wrapper around both checks:
Handle dust accounts
Make sure that sending and receiving accounts aren't dust accounts. Use T::MinBalance::get()
:
Examples
Tests in the
reward-coin
example pallet.
Resources
Last updated