ic-canister-runtime
Library to abstract the canister runtime so that code making requests to canisters can be reused, e.g.:
- in production using
ic_cdk, - in unit tests by mocking this crate's
Runtimetrait, - in integration tests by implementing this trait for PocketIC yourself or using the
PokcetIcRuntimeimplementation from theic-pocket-canister-runtimecrate.
Usage
Add this to your Cargo.toml (see crates.io for the latest version):
= "0.1.0"
Then, use the library to abstract your code making requests to canisters as follows:
use ;
// This runtime makes calls to canisters deployed on the Internet Computer using the `ic-cdk`
let runtime = new;
// Make a request to the `http_request` example canister's `make_http_post_request` endpoint
// See: https://github.com/dfinity/canhttp/tree/main/examples/http_canister
let http_request_result: String = runtime
.update_call
.await
.expect;
assert!;
assert!;
The same code can then be re-used for example in unit tests by simply changing the runtime:
use ;
// Use a mock runtime for unit testing
let runtime = new
.add_stub_response;
// The code below is the same as in the previous example
let http_request_result: String = runtime
.update_call
.await
.expect;
assert!;
assert!;
See the Rust documentation for more details as well as the ic-pocket-canister-runtime and ic-agent-canister-runtime crates for some further implementations of the Runtime trait.
Cargo Features
Feature wallet
Provides the CyclesWalletRuntime implementation which allows routing update calls to a canister through a cycles wallet to attach cycles to them.
License
This project is licensed under the Apache License 2.0.