pub struct MockCanisterCaller<F: Fn(&Principal, &str, Vec<u8>) -> Vec<u8> + Send + Sync> { /* private fields */ }Expand description
A mock implementation of CanisterCaller for testing purposes.
This struct allows you to simulate canister calls by providing a transformation function that takes the canister ID, method name, and arguments, and returns a response.
§Example
ⓘ
use anda_engine::context::mock::MockCanisterCaller;
use anda_core::CanisterCaller;
use candid::{encode_args, CandidType, Deserialize, Principal};
#[derive(CandidType, Deserialize, Debug, PartialEq)]
struct TestResponse {
canister: Principal,
method: String,
args: Vec<u8>,
}
#[tokio::test]
async fn test_mock_canister_caller() {
let canister_id = Principal::anonymous();
let empty_args = encode_args(()).unwrap();
let caller = MockCanisterCaller::new(|canister, method, args| {
let response = TestResponse {
canister: canister.clone(),
method: method.to_string(),
args,
};
candid::encode_args((response,)).unwrap()
});
let res: TestResponse = caller
.canister_query(&canister_id, "canister_query", ())
.await
.unwrap();
assert_eq!(res.canister, canister_id);
assert_eq!(res.method, "canister_query");
assert_eq!(res.args, empty_args);
let res: TestResponse = caller
.canister_update(&canister_id, "canister_update", ())
.await
.unwrap();
assert_eq!(res.canister, canister_id);
assert_eq!(res.method, "canister_update");
assert_eq!(res.args, empty_args);
}Implementations§
Source§impl<F> MockCanisterCaller<F>
impl<F> MockCanisterCaller<F>
Trait Implementations§
Source§impl<F> CanisterCaller for MockCanisterCaller<F>
impl<F> CanisterCaller for MockCanisterCaller<F>
Source§async fn canister_query<In: ArgumentEncoder + Send, Out: CandidType + for<'a> Deserialize<'a>>(
&self,
canister: &Principal,
method: &str,
args: In,
) -> Result<Out, BoxError>
async fn canister_query<In: ArgumentEncoder + Send, Out: CandidType + for<'a> Deserialize<'a>>( &self, canister: &Principal, method: &str, args: In, ) -> Result<Out, BoxError>
Performs a query call to a canister (read-only, no state changes) Read more
Source§async fn canister_update<In: ArgumentEncoder + Send, Out: CandidType + for<'a> Deserialize<'a>>(
&self,
canister: &Principal,
method: &str,
args: In,
) -> Result<Out, BoxError>
async fn canister_update<In: ArgumentEncoder + Send, Out: CandidType + for<'a> Deserialize<'a>>( &self, canister: &Principal, method: &str, args: In, ) -> Result<Out, BoxError>
Performs an update call to a canister (may modify state) Read more
Auto Trait Implementations§
impl<F> Freeze for MockCanisterCaller<F>where
F: Freeze,
impl<F> RefUnwindSafe for MockCanisterCaller<F>where
F: RefUnwindSafe,
impl<F> Send for MockCanisterCaller<F>
impl<F> Sync for MockCanisterCaller<F>
impl<F> Unpin for MockCanisterCaller<F>where
F: Unpin,
impl<F> UnsafeUnpin for MockCanisterCaller<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for MockCanisterCaller<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
impl<T> Fruit for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more