Struct concordium_std::test_infrastructure::TestHost
source · [−]pub struct TestHost<State> { /* private fields */ }
Expand description
A Host
implementation used for unit testing smart contracts.
The host provides a way to set up mock responses to transfers, and to contract invocations. This allows testing a specific entrypoint of a contract in isolation, assuming its dependents behave in the way specified by the supplied mock functions.
Additionally, this host provides some inspection capability so that after execution of an entrypoint tests can observe which accounts or contracts were affected.
Implementations
sourceimpl<State: Serial + DeserialWithState<TestStateApi>> TestHost<State>
impl<State: Serial + DeserialWithState<TestStateApi>> TestHost<State>
sourcepub fn new(state: State, state_builder: StateBuilder<TestStateApi>) -> Self
pub fn new(state: State, state_builder: StateBuilder<TestStateApi>) -> Self
sourcepub fn state_builder(&mut self) -> &mut StateBuilder<TestStateApi>
pub fn state_builder(&mut self) -> &mut StateBuilder<TestStateApi>
Retrieve a reference to the underlying state builder.
sourcepub fn setup_mock_entrypoint(
&mut self,
to: ContractAddress,
method: OwnedEntrypointName,
handler: MockFn<State>
)
pub fn setup_mock_entrypoint(
&mut self,
to: ContractAddress,
method: OwnedEntrypointName,
handler: MockFn<State>
)
Set up a mock entrypoint for handling calls to invoke_contract
.
If you set up multiple handlers for the same entrypoint (to, method), then the latest handler will be used.
sourcepub fn set_self_balance(&mut self, amount: Amount)
pub fn set_self_balance(&mut self, amount: Amount)
Set the contract balance. NB: This should be the sum of the contract’s initial balance and the amount you wish to invoke it with.
Example:
host.set_self_balance(Amount::from_ccd(10));
contract_receive(
&ctx,
&mut host,
// This amount is _not_ added to the balance of the contract,
// so calling `host.self_balance()` will return `10` initially.
// When a contract is executed by the node the amount that is being sent (`5`)
// is added to the balance of the contract, so that `host.self_balance()`
// already observes it.
Amount::from_ccd(5),
);
sourcepub fn transfer_occurred(&self, receiver: &AccountAddress, amount: Amount) -> bool
pub fn transfer_occurred(&self, receiver: &AccountAddress, amount: Amount) -> bool
Check whether a given transfer occured.
sourcepub fn get_transfers(&self) -> Vec<(AccountAddress, Amount)>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn get_transfers(&self) -> Vec<(AccountAddress, Amount)>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Get a list of all transfers that have occurred, in the order they occurred in.
sourcepub fn get_transfers_to(&self, account: AccountAddress) -> Vec<Amount>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn get_transfers_to(&self, account: AccountAddress) -> Vec<Amount>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Get a list of all transfers to a specific account.
sourcepub fn make_account_missing(&mut self, account: AccountAddress)
pub fn make_account_missing(&mut self, account: AccountAddress)
Set an account to be missing. Any transfers to this account will result in an TransferError::MissingAccount error.
This differs from the default, where all accounts are assumed to exist.
sourceimpl<State: StateClone<TestStateApi>> TestHost<State>
impl<State: StateClone<TestStateApi>> TestHost<State>
sourcepub fn with_rollback<R, E>(
&mut self,
call: impl FnOnce(&mut TestHost<State>) -> Result<R, E>
) -> Result<R, E>
pub fn with_rollback<R, E>(
&mut self,
call: impl FnOnce(&mut TestHost<State>) -> Result<R, E>
) -> Result<R, E>
Helper function for invoking receive methods that respects the
transactional nature of invocations. That is, if the invocation
returns Err(_)
, then the host and state is rolled back to a
checkpoint just before the invocation.
Trait Implementations
sourceimpl<State: Serial + DeserialWithState<TestStateApi> + StateClone<TestStateApi>> HasHost<State> for TestHost<State>
impl<State: Serial + DeserialWithState<TestStateApi> + StateClone<TestStateApi>> HasHost<State> for TestHost<State>
sourcefn invoke_transfer(
&self,
receiver: &AccountAddress,
amount: Amount
) -> TransferResult
fn invoke_transfer(
&self,
receiver: &AccountAddress,
amount: Amount
) -> TransferResult
Perform a transfer to the given account if the contract has sufficient balance.
By default, all accounts are assumed to exist, and transfers to them
will succeed (provided sufficient balance).
Use make_account_missing
to test out transfers to accounts not on
chain.
Possible errors:
- TransferError::AmountTooLarge: Contract has insufficient funds.
- TransferError::MissingAccount: Attempted transfer to an account
set as missing with
make_account_missing
.
sourcefn invoke_contract_raw(
&mut self,
to: &ContractAddress,
parameter: Parameter<'_>,
method: EntrypointName<'_>,
amount: Amount
) -> CallContractResult<Self::ReturnValueType>
fn invoke_contract_raw(
&mut self,
to: &ContractAddress,
parameter: Parameter<'_>,
method: EntrypointName<'_>,
amount: Amount
) -> CallContractResult<Self::ReturnValueType>
Invoke a contract entrypoint.
This uses the mock entrypoints set up with
setup_mock_entrypoint
. The method will fail with a panic
if no responses were set for the given contract address and method.
If the invocation results in Err(_)
, the host and state will be rolled
back. This means that the state and the logs of, e.g., transactions will
look as if the invocation never occurred. See also
TestHost::with_rollback
.
sourcefn invoke_contract_raw_read_only(
&self,
to: &ContractAddress,
parameter: Parameter<'_>,
method: EntrypointName<'_>,
amount: Amount
) -> ReadOnlyCallContractResult<Self::ReturnValueType>
fn invoke_contract_raw_read_only(
&self,
to: &ContractAddress,
parameter: Parameter<'_>,
method: EntrypointName<'_>,
amount: Amount
) -> ReadOnlyCallContractResult<Self::ReturnValueType>
Invoke a contract entrypoint.
This uses the mock entrypoints set up with
setup_mock_entrypoint
. The method will fail with a panic
if no responses were set for the given contract address and method.
If the invocation results in Err(_)
, the host and state will be rolled
back. This means that the state and the logs of, e.g., transactions will
look as if the invocation never occurred. See also
TestHost::with_rollback
.
sourcefn self_balance(&self) -> Amount
fn self_balance(&self) -> Amount
Get the contract balance.
This can be set with set_self_balance
and defaults to 0.
sourcefn state_builder(&mut self) -> &mut StateBuilder<Self::StateApiType>
fn state_builder(&mut self) -> &mut StateBuilder<Self::StateApiType>
Get the state builder.
sourcefn state_and_builder(
&mut self
) -> (&mut State, &mut StateBuilder<Self::StateApiType>)
fn state_and_builder(
&mut self
) -> (&mut State, &mut StateBuilder<Self::StateApiType>)
Get the state and the state builder.
type ReturnValueType = Cursor<Vec<u8, Global>>
type ReturnValueType = Cursor<Vec<u8, Global>>
type StateApiType = TestStateApi
type StateApiType = TestStateApi
The type of low-level state that is associated with the host. This provides access to low-level state operations. Read more
sourcefn commit_state(&mut self)
fn commit_state(&mut self)
Make sure the contract state is fully written out, so that any changes that were done in-memory up to the point in contract execution are reflected in the actual contract state maintained by the node. Read more
sourcefn invoke_contract<P: Serial>(
&mut self,
to: &ContractAddress,
parameter: &P,
method: EntrypointName<'_>,
amount: Amount
) -> CallContractResult<Self::ReturnValueType>
fn invoke_contract<P: Serial>(
&mut self,
to: &ContractAddress,
parameter: &P,
method: EntrypointName<'_>,
amount: Amount
) -> CallContractResult<Self::ReturnValueType>
Like invoke_contract_raw
, except that the
parameter is automatically serialized. If the parameter already
implements AsRef<[u8]>
or can be equivalently cheaply
converted to a byte array, then
invoke_contract_raw
should be
used, since it avoids intermediate allocations. Read more
sourcefn invoke_contract_read_only<P: Serial>(
&self,
to: &ContractAddress,
parameter: &P,
method: EntrypointName<'_>,
amount: Amount
) -> ReadOnlyCallContractResult<Self::ReturnValueType>
fn invoke_contract_read_only<P: Serial>(
&self,
to: &ContractAddress,
parameter: &P,
method: EntrypointName<'_>,
amount: Amount
) -> ReadOnlyCallContractResult<Self::ReturnValueType>
Like invoke_contract_raw
, except that the
parameter is automatically serialized. If the parameter already
implements AsRef<[u8]>
or can be equivalently cheaply
converted to a byte array, then
invoke_contract_raw
should be
used, since it avoids intermediate allocations. Read more
Auto Trait Implementations
impl<State> !RefUnwindSafe for TestHost<State>
impl<State> !Send for TestHost<State>
impl<State> !Sync for TestHost<State>
impl<State> Unpin for TestHost<State> where
State: Unpin,
impl<State> !UnwindSafe for TestHost<State>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more