Crates.io | pocket-ic |
lib.rs | pocket-ic |
version | 3.1.0 |
source | src |
created_at | 2023-10-12 14:31:12.300898 |
updated_at | 2024-05-02 11:11:17.289462 |
description | PocketIC: A Canister Smart Contract Testing Platform |
homepage | |
repository | https://github.com/dfinity/ic |
max_upload_size | |
id | 1001364 |
size | 104,072 |
PocketIC is a local canister testing solution for the Internet Computer.
This testing library works together with the PocketIC server, allowing you to interact with your local IC instances and the canisters thereon.
With PocketIC Rust, testing canisters is as simple as calling Rust functions. Here is a simple example:
use candid::encode_one;
use pocket_ic::PocketIc;
#[test]
fn test_counter_canister() {
let pic = PocketIc::new();
// Create an empty canister as the anonymous principal and add cycles.
let canister_id = pic.create_canister();
pic.add_cycles(canister_id, 2_000_000_000_000);
let wasm_bytes = load_counter_wasm(...);
pic.install_canister(canister_id, wasm_bytes, vec![], None);
// 'inc' is a counter canister method.
call_counter_canister(&pic, canister_id, "inc");
// Check if it had the desired effect.
let reply = call_counter_canister(&pic, canister_id, "read");
assert_eq!(reply, WasmResult::Reply(vec![0, 0, 0, 1]));
}
fn call_counter_canister(pic: &PocketIc, canister_id: CanisterId, method: &str) -> WasmResult {
pic.update_call(canister_id, Principal::anonymous(), method, encode_one(()).unwrap())
.expect("Failed to call counter canister")
}
POCKET_IC_BIN
environment variable before running your tests.cargo add pocket-ic
.use pocket_ic::PocketIc
, and create a new PocketIC instance with let pic = PocketIc::new()
in your Rust code and start testing!For a simple but complete example with the counter canister, see here. For an example with cross canister calls on two different subnets with the ledger canister, see here.
To see a minimalistic setup of PocketIC in a Rust project, check out the ICP Hello World Rust repository.
For larger test suites with more complex test setups, consider the OpenChat integration test suite. Note that instances are shared among test cases there, which is not recommended in general.
If you decide to contribute, we encourage you to announce it on the Forum!