This page covers the different testing utils/libraries that we have for easier unit testing in Rust.
To create a new crypto hash:
"ADns6sqVyFLRZbSMCGdzUiUPaDDtjTmKCWzR8HxWsfDU".parse().unwrap();
Also, prefer doing parse + unwrap:
let alice: AccountId = "alice.near".parse().unwrap();
In memory signer (generates the key based on a seed). There is a slight preference to use the seed that is matching the account name.
This will create a signer for account 'test' using 'test' as a seed.
let signer: InMemoryValidatorSigner = create_test_signer("test");
Use TestBlockBuilder
to create the block that you need. This class allows you to set custom values for most of the fields.
let test_block = test_utils::TestBlockBuilder::new(prev, signer).height(33).build();
Use the in-memory test store in tests:
let store = create_test_store();
See usages of MockEpochManager. Note that this is deprecated. Try to use EpochManager itself wherever possible.
You can use the KeyValueRuntime (instead of the Nightshade one):
KeyValueRuntime::new(store, &epoch_manager);
No fakes or mocks.
TestEnv - for testing multiple clients (without network):
TestEnvBuilder::new(genesis).client(vec!["aa"]).validators(..).epoch_managers(..).build();
To create a PeerManager handler:
let pm = peer_manager::testonly::start(...).await;
To connect to others:
pm.connect_to(&pm2.peer_info).await;
To wait/handle a given event (as a lot of network code is running in an async fashion):
pm.events.recv_util(|event| match event {...}).await;
In chain/chain/src/test_utils.rs:
// Creates 1-validator (test): chain, KVRuntime and a signer
let (chain, runtime, signer) = setup();
In chain/client/src/test_utils.rs
let (block, client, view_client) = setup(MANY_FIELDS);