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

Enums

  • 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.
  • An error that is raised when operating with Seek, Write, Read, or HasStateEntry trait methods of the TestStateApi type.

Type Definitions