👎Deprecated since 8.1.0: Deprecated in favor of concordium-smart-contract-testing.
Expand description
The test infrastructure module provides alternative implementations of
HasInitContext, HasReceiveContext, HasParameter, HasStateApi,
and HasHost traits intended for testing.
They allow writing unit tests directly in contract modules with little to no external tooling, depending on what is required.
§Example
// Some contract
#[init(contract = "noop")]
fn contract_init<S: HasStateApi>(
ctx: &impl HasInitContext,
state_builder: &mut StateBuilder<S>,
) -> InitResult<State> {
// ...
}
#[receive(contract = "noop", name = "receive", payable, enable_logger, mutable)]
fn contract_receive<S: HasStateApi>(
ctx: &impl HasReceiveContext,
host: &mut impl HasHost<State, StateApiType = S>,
amount: Amount,
logger: &mut impl HasLogger,
) -> ReceiveResult<MyReturnValue> {
// ...
}
#[cfg(test)]
mod tests {
use super::*;
use concordium_std::test_infrastructure::*;
#[test]
fn test_init() {
let mut ctx = TestInitContext::empty();
let mut state_builder = TestStateBuilder::new();
ctx.set_init_origin(AccountAddress([0u8; 32]));
let result = contract_init(&ctx, &mut state_builder);
// claim!(...)
}
#[test]
fn test_receive() {
let mut ctx = TestReceiveContext::empty();
let mut host = TestHost::new(State::new(), TestStateBuilder::new());
ctx.set_owner(AccountAddress([0u8; 32]));
// ...
let mut logger = TestLogger::init();
host.setup_mock_entrypoint(
ContractAddress {
index: 0,
subindex: 0,
},
OwnedEntrypointName::new_unchecked("get".into()),
MockFn::returning_ok(MyReturnValue::new()),
);
let result: ReceiveResult<MyReturnValue> =
contract_receive(&ctx, &mut host, Amount::zero(), &mut logger);
// claim!(...)
}
}Structs§
- MockFn
Deprecated - Holds a function used for mocking invocations of contracts with
invoke_contract. - Test
Chain Meta Deprecated - Placeholder for the context chain meta data.
All the fields are optionally set and the getting an unset field will result
in test failing.
For most cases it is used as part of either
TestInitContextorTestReceiveContext. Use only in unit tests! - Test
Context Deprecated - Context used for testing. The type parameter C is used to determine whether this will be an init or receive context.
- Test
Crypto Primitives Deprecated - A
HasCryptoPrimitivesimplementation used for unit testing smart contracts. - Test
Host Deprecated - A
Hostimplementation used for unit testing smart contracts. - Test
Iterator Deprecated - Test
Logger Deprecated - A logger that simply accumulates all the logged items to be inspected at the end of execution.
- Test
Parameter Cursor Deprecated - Test parameter cursor. Should not be constructed directly, use TestReceiveContext or TestInitContext.
- Test
Policy Deprecated - Policy type used by init and receive contexts for testing.
This type should not be used directly, but rather through
its
HasPolicyinterface. - Test
State Api Deprecated - A state api used for testing. Implements
HasStateApi. - Test
State Entry Deprecated - A state entry used for testing. Implements
HasStateEntry.
Enums§
- Test
State Entry Data Deprecated - A wrapper for the data stored in
TestStateEntry, which is used to match the semantics of the host functions. Specifically, it is used to ensure that interactions with a deleted entry result in a error. - Test
State Error Deprecated - An error that is raised when operating with
Seek,Write,Read, orHasStateEntrytrait methods of theTestStateApitype.
Type Aliases§
- Test
Init Context Deprecated - Placeholder for the initial context. All the fields can be set optionally
and the getting an unset field will result in calling
fail!. Use only in tests! - Test
Receive Context Deprecated - Placeholder for the receiving context. All the fields can be set optionally
and the getting an unset field will result in calling
fail!. Use only in tests! - Test
State Builder Deprecated - Test
State MapIter Deprecated - An alias for
StateMapIterthat fixes theHasStateApitype toTestStateApi. - Test
State MapIter Mut Deprecated - An alias for
StateMapIterMutthat fixes theHasStateApitype toTestStateApi. - Test
State SetIter Deprecated - An alias for
StateSetIterthat fixes theHasStateApitype toTestStateApi.