Module concordium_std::test_infrastructure[][src]

Expand description

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

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!

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

Contract state for testing, mimicking the operations the scheduler allows, including the limit on the size of the maximum size of the contract state.

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

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

Enums

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

An error that is raised when operating with Seek, Write, or Read trait methods of the ContractStateTest type.

Type Definitions

A borrowed instantiation of ContractStateTest.

An owned variant that can be more convenient for testing since the type itself owns the data.

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!

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!