[][src]Module concordium_std::test_infrastructure

The test infrastructure module provides alternative implementations of HasInitContext, HasReceiveContext, HasParameter, HasActions, and HasContractState 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<I: HasInitContext, L: HasLogger>(
    ctx: &I,
) -> InitResult<State> { ... }

#[receive(contract = "noop", name = "receive", payable, enable_logger)]
fn contract_receive<R: HasReceiveContext, L: HasLogger, A: HasActions>(
    ctx: &R,
    amount: Amount,
    logger: &mut L,
    state: &mut State,
) -> ReceiveResult<A> { ... }

#[cfg(test)]
mod tests {
    use super::*;
    use concordium_sc_base::test_infrastructure::*;
    #[test]
    fn test_init() {
        let mut ctx = InitContextTest::empty();
        ctx.set_init_origin(AccountAddress([0u8; 32]));
        ...
        let result = contract_init(&ctx);
        claim!(...)
        ...
    }

    #[test]
    fn test_receive() {
        let mut ctx = ReceiveContextTest::empty();
        ctx.set_owner(AccountAddress([0u8; 32]));
        ...
        let mut logger = LogRecorder::init();
        let result: ReceiveResult<ActionsTree> = contract_receive(&ctx, 0, &mut logger, state);
        claim!(...)
        ...
    }
}

Structs

ChainMetaTest

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 InitContextTest or ReceiveContextTest. Use only in unit tests!

ContextTest

Context used for testing. The type parameter C is used to determine whether this will be an init or receive context.

LogRecorder

A logger that simply accumulates all the logged items to be inspected at the end of execution.

TestPolicy

Policy type used by init and receive contexts for testing. This type should not be used directly, but rather through its HasPolicy interface.

Enums

ActionsTree

An actions tree, used to provide a simpler presentation for testing.

Type Definitions

InitContextTest

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!

ReceiveContextTest

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!