This library provides an alternative to ic-cdk
that can help developers write canisters
and unit test them in their Rust code.
Add this to your Cargo.toml
toml
ic-kit = "0.4.0"
```rust use ickit::macros::*; use ickit::*;
fn whoami() -> Principal { let ic = get_context(); ic.caller() }
async fn sendcycles(canisterid: Principal, cycles: u64) -> Result<(), String> { let ic = getcontext(); ic.callwithpayment(canisterid, "walletaccept", (), cycles) .await .maperr(|(code, msg)| format!("Call failed with code={}: {}", code as u8, msg)) }
mod tests { use super::*;
#[test]
fn test_whoami() {
MockContext::new()
.with_caller(mock_principals::alice())
.inject();
assert_eq!(whoami(), mock_principals::alice());
}
#[async_test]
async fn test_send_cycles() {
// Create a context that just consumes 1000 cycles from all the inter-canister calls and
// returns "()" in response.
let ctx = MockContext::new()
.with_consume_cycles_handler(1000)
.inject();
// Init a watcher at this point that will track all of the calls made from now on.
let watcher = ctx.watch();
send_cycles(mock_principals::xtc(), 5000).await.unwrap();
assert_eq!(watcher.cycles_consumed(), 1000);
assert_eq!(watcher.cycles_sent(), 5000);
}
}
```