pub struct MockContext { /* private fields */ }Expand description
A context that could be used to fake/control the behaviour of the IC when testing the canister.
Implementations§
Source§impl MockContext
impl MockContext
Sourcepub fn watch(&self) -> &Watcher
pub fn watch(&self) -> &Watcher
Reset the current watcher on the MockContext and return a reference to it.
Sourcepub fn with_id(self, id: Principal) -> Self
pub fn with_id(self, id: Principal) -> Self
Set the ID of the canister.
§Example
use ic_kit::*;
let id = Principal::from_text("ai7t5-aibaq-aaaaa-aaaaa-c").unwrap();
MockContext::new()
.with_id(id.clone())
.inject();
assert_eq!(ic::id(), id);Sourcepub fn with_balance(self, cycles: u64) -> Self
pub fn with_balance(self, cycles: u64) -> Self
Set the balance of the canister.
§Example
use ic_kit::*;
MockContext::new()
.with_balance(1000)
.inject();
assert_eq!(ic::balance(), 1000);Sourcepub fn with_caller(self, caller: Principal) -> Self
pub fn with_caller(self, caller: Principal) -> Self
Set the caller for the current call.
§Example
use ic_kit::*;
let alice = Principal::from_text("ai7t5-aibaq-aaaaa-aaaaa-c").unwrap();
MockContext::new()
.with_caller(alice.clone())
.inject();
assert_eq!(ic::caller(), alice);Sourcepub fn with_msg_cycles(self, cycles: u64) -> Self
pub fn with_msg_cycles(self, cycles: u64) -> Self
Make the given amount of cycles available for the call. This amount of cycles will be deduced if the call accepts them or will be refunded. If the canister accepts any cycles the balance of the canister will be increased.
§Example
use ic_kit::*;
MockContext::new()
.with_msg_cycles(1000)
.inject();
assert_eq!(ic::msg_cycles_available(), 1000);
ic::msg_cycles_accept(300);
assert_eq!(ic::msg_cycles_available(), 700);Sourcepub fn with_data<T: 'static>(self, data: T) -> Self
pub fn with_data<T: 'static>(self, data: T) -> Self
Initialize the context with the given value inserted in the storage.
§Example
use ic_kit::*;
MockContext::new()
.with_data(String::from("Hello"))
.inject();
assert_eq!(ic::get::<String>(), &"Hello".to_string());Sourcepub fn with_stable<T>(self, data: T) -> Selfwhere
T: ArgumentEncoder + Serialize,
pub fn with_stable<T>(self, data: T) -> Selfwhere
T: ArgumentEncoder + Serialize,
Initialize the context with the given value inserted into the stable storage.
§Example
use ic_kit::*;
MockContext::new()
.with_stable(("Bella".to_string(), ))
.inject();
assert_eq!(ic::stable_restore::<(String, )>(), Ok(("Bella".to_string(), )));Sourcepub fn with_certified_data(self, data: Vec<u8>) -> Self
pub fn with_certified_data(self, data: Vec<u8>) -> Self
Set the certified data of the canister.
Sourcepub fn with_consume_cycles_handler(self, cycles: u64) -> Self
pub fn with_consume_cycles_handler(self, cycles: u64) -> Self
Creates a mock context with a default handler that accepts the given amount of cycles on every request.
Sourcepub fn with_expect_cycles_handler(self, cycles: u64) -> Self
pub fn with_expect_cycles_handler(self, cycles: u64) -> Self
Create a mock context with a default handler that expects this amount of cycles to be passed to it.
Sourcepub fn with_refund_cycles_handler(self, cycles: u64) -> Self
pub fn with_refund_cycles_handler(self, cycles: u64) -> Self
Creates a mock context with a default handler that refunds the given amount of cycles on every request.
Sourcepub fn with_constant_return_handler<T: CandidType>(self, value: T) -> Self
pub fn with_constant_return_handler<T: CandidType>(self, value: T) -> Self
Create a mock context with a default handler that returns the given value.
Sourcepub fn with_handler<T: 'static + CallHandler>(self, handler: T) -> Self
pub fn with_handler<T: 'static + CallHandler>(self, handler: T) -> Self
Add the given handler to the handlers pipeline.
Source§impl MockContext
impl MockContext
Sourcepub fn call_state_reset(&self)
pub fn call_state_reset(&self)
Reset the state after a call.
Sourcepub fn clear_storage(&self)
pub fn clear_storage(&self)
Clear the storage.
Sourcepub fn update_balance(&self, cycles: u64)
pub fn update_balance(&self, cycles: u64)
Update the balance of the canister.
Sourcepub fn update_msg_cycles(&self, cycles: u64)
pub fn update_msg_cycles(&self, cycles: u64)
Update the cycles of the next message.
Sourcepub fn update_caller(&self, caller: Principal)
pub fn update_caller(&self, caller: Principal)
Update the caller for the next message.
Sourcepub fn update_id(&self, canister_id: Principal)
pub fn update_id(&self, canister_id: Principal)
Update the canister id the call happens from for the next message.
Sourcepub fn get_certified_data(&self) -> Option<Vec<u8>>
pub fn get_certified_data(&self) -> Option<Vec<u8>>
Return the certified data set on the canister.
Sourcepub fn use_handler<T: 'static + CallHandler>(&mut self, handler: T)
pub fn use_handler<T: 'static + CallHandler>(&mut self, handler: T)
Add the given handler to the call handlers pipeline.
Sourcepub fn clear_handlers(&mut self)
pub fn clear_handlers(&mut self)
Remove all of the call handlers that are already registered to this context.
Trait Implementations§
Source§impl Context for MockContext
impl Context for MockContext
Source§fn msg_cycles_available(&self) -> u64
fn msg_cycles_available(&self) -> u64
Source§fn msg_cycles_accept(&self, cycles: u64) -> u64
fn msg_cycles_accept(&self, cycles: u64) -> u64
Source§fn msg_cycles_refunded(&self) -> u64
fn msg_cycles_refunded(&self) -> u64
Source§fn stable_store<T>(&self, data: T) -> Result<(), Error>where
T: ArgumentEncoder,
fn stable_store<T>(&self, data: T) -> Result<(), Error>where
T: ArgumentEncoder,
Source§fn stable_restore<T>(&self) -> Result<T, String>where
T: for<'de> ArgumentDecoder<'de>,
fn stable_restore<T>(&self) -> Result<T, String>where
T: for<'de> ArgumentDecoder<'de>,
Source§fn call_raw<S: Into<String>>(
&'static self,
id: Principal,
method: S,
args_raw: Vec<u8>,
cycles: u64,
) -> CallResponse<Vec<u8>>
fn call_raw<S: Into<String>>( &'static self, id: Principal, method: S, args_raw: Vec<u8>, cycles: u64, ) -> CallResponse<Vec<u8>>
Source§fn set_certified_data(&self, data: &[u8])
fn set_certified_data(&self, data: &[u8])
Source§fn data_certificate(&self) -> Option<Vec<u8>>
fn data_certificate(&self) -> Option<Vec<u8>>
Source§fn spawn<F: 'static + Future<Output = ()>>(&mut self, future: F)
fn spawn<F: 'static + Future<Output = ()>>(&mut self, future: F)
Source§fn stable_size(&self) -> u32
fn stable_size(&self) -> u32
Source§fn stable_grow(&self, new_pages: u32) -> Result<u32, StableMemoryError>
fn stable_grow(&self, new_pages: u32) -> Result<u32, StableMemoryError>
Source§fn stable_write(&self, offset: u32, buf: &[u8])
fn stable_write(&self, offset: u32, buf: &[u8])
Source§fn stable_read(&self, offset: u32, buf: &mut [u8])
fn stable_read(&self, offset: u32, buf: &mut [u8])
Source§fn with<T: 'static + Default, U, F: FnOnce(&T) -> U>(&self, callback: F) -> U
fn with<T: 'static + Default, U, F: FnOnce(&T) -> U>(&self, callback: F) -> U
T to the callback, stores the default value
if not present, and return the transformation.Source§fn maybe_with<T: 'static, U, F: FnOnce(&T) -> U>(
&self,
callback: F,
) -> Option<U>
fn maybe_with<T: 'static, U, F: FnOnce(&T) -> U>( &self, callback: F, ) -> Option<U>
T to the callback, and return the mapped
value.Source§fn with_mut<T: 'static + Default, U, F: FnOnce(&mut T) -> U>(
&self,
callback: F,
) -> U
fn with_mut<T: 'static + Default, U, F: FnOnce(&mut T) -> U>( &self, callback: F, ) -> U
T to the callback, stores the default value
if not present, and return the transformation.Source§fn maybe_with_mut<T: 'static, U, F: FnOnce(&mut T) -> U>(
&self,
callback: F,
) -> Option<U>
fn maybe_with_mut<T: 'static, U, F: FnOnce(&mut T) -> U>( &self, callback: F, ) -> Option<U>
T to the callback, and return the callback’s
result.Source§fn take<T: 'static>(&self) -> Option<T>
fn take<T: 'static>(&self) -> Option<T>
Source§fn swap<T: 'static>(&self, value: T) -> Option<T>
fn swap<T: 'static>(&self, value: T) -> Option<T>
T with the new provided one and return the old one if
any.