Module concordium_std::test_infrastructure
source · 👎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§
- MockFnDeprecatedHolds a function used for mocking invocations of contracts with
invoke_contract
. - TestChainMetaDeprecatedPlaceholder 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
TestInitContext
orTestReceiveContext
. Use only in unit tests! - TestContextDeprecatedContext used for testing. The type parameter C is used to determine whether this will be an init or receive context.
- TestCryptoPrimitivesDeprecatedA
HasCryptoPrimitives
implementation used for unit testing smart contracts. - TestHostDeprecatedA
Host
implementation used for unit testing smart contracts. - TestIteratorDeprecated
- TestLoggerDeprecatedA logger that simply accumulates all the logged items to be inspected at the end of execution.
- TestParameterCursorDeprecatedTest parameter cursor. Should not be constructed directly, use TestReceiveContext or TestInitContext.
- TestPolicyDeprecatedPolicy type used by init and receive contexts for testing. This type should not be used directly, but rather through its
HasPolicy
interface. - TestStateApiDeprecatedA state api used for testing. Implements
HasStateApi
. - TestStateEntryDeprecatedA state entry used for testing. Implements
HasStateEntry
.
Enums§
- TestStateEntryDataDeprecatedA 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. - TestStateErrorDeprecatedAn error that is raised when operating with
Seek
,Write
,Read
, orHasStateEntry
trait methods of theTestStateApi
type.
Type Aliases§
- TestInitContextDeprecatedPlaceholder 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! - TestReceiveContextDeprecatedPlaceholder 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! - TestStateBuilderDeprecated
- TestStateMapIterDeprecated
- TestStateMapIterMutDeprecated
- TestStateSetIterDeprecated