Struct ic_kit::MockContext[][src]

pub struct MockContext { /* fields omitted */ }
Expand description

A context that could be used to fake/control the behaviour of the IC when testing the canister.

Implementations

Create a new mock context which could be injected for testing.

Reset the current watcher on the MockContext and return a reference to it.

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();

let ic = get_context();
assert_eq!(ic.id(), id);

Set the balance of the canister.

Example

use ic_kit::*;

MockContext::new()
    .with_balance(1000)
    .inject();

let ic = get_context();
assert_eq!(ic.balance(), 1000);

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();

let ic = get_context();
assert_eq!(ic.caller(), alice);

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();

let ic = get_context();
assert_eq!(ic.msg_cycles_available(), 1000);
ic.msg_cycles_accept(300);
assert_eq!(ic.msg_cycles_available(), 700);

Initialize the context with the given value inserted in the storage.

Example

use ic_kit::*;

MockContext::new()
    .with_data(String::from("Hello"))
    .inject();

let ic = get_context();
assert_eq!(ic.get::<String>(), &"Hello".to_string());

Initialize the context with the given value inserted into the stable storage.

Example

use ic_kit::*;

MockContext::new()
    .with_stable(("Bella".to_string(), ))
    .inject();

let ic = get_context();
assert_eq!(ic.stable_restore::<(String, )>(), Ok(("Bella".to_string(), )));

Set the certified data of the canister.

Creates a mock context with a default handler that accepts the given amount of cycles on every request.

Create a mock context with a default handler that expects this amount of cycles to be passed to it.

Creates a mock context with a default handler that refunds the given amount of cycles on every request.

Create a mock context with a default handler that returns the given value.

Add the given handler to the handlers pipeline.

Use this context as the default context for this thread.

Sign a data and return the certificate, this is the method used in set_certified_data to set the data certificate for the given certified data.

Reset the state after a call.

Clear the storage.

Update the balance of the canister.

Update the cycles of the next message.

Update the caller for the next message.

Return the certified data set on the canister.

Add the given handler to the call handlers pipeline.

Remove all of the call handlers that are already registered to this context.

Trait Implementations

Trap the code.

Print a message.

ID of the current canister.

The time in nanoseconds.

The balance of the canister.

The caller who has invoked this method on the canister.

Return the number of available cycles that is sent by the caller.

Accept the given amount of cycles, returns the actual amount of accepted cycles.

Return the cycles that were sent back by the canister that was just called. This method should only be called right after an inter-canister call. Read more

Store the given data to the storage.

Return the data associated with the given type. If the data is not present the default value of the type is returned. Read more

Return a mutable reference to the given data type, if the data is not present the default value of the type is constructed and stored. The changes made to the data during updates is preserved. Read more

Remove the data associated with the given data type.

Store the given data to the stable storage.

Restore the data from the stable storage. If the data is not already stored the None value is returned. Read more

Perform a call.

Set the certified data of the canister, this method traps if data.len > 32.

Returns the data certificate authenticating certified_data set by this canister.

Perform the call and return the response.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.