👎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§

  • MockFnDeprecated
    Holds a function used for mocking invocations of contracts with invoke_contract.
  • TestChainMetaDeprecated
    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 TestInitContext or TestReceiveContext. Use only in unit tests!
  • TestContextDeprecated
    Context used for testing. The type parameter C is used to determine whether this will be an init or receive context.
  • A HasCryptoPrimitives implementation used for unit testing smart contracts.
  • TestHostDeprecated
    A Host implementation used for unit testing smart contracts.
  • TestIteratorDeprecated
  • TestLoggerDeprecated
    A logger that simply accumulates all the logged items to be inspected at the end of execution.
  • Test parameter cursor. Should not be constructed directly, use TestReceiveContext or TestInitContext.
  • TestPolicyDeprecated
    Policy type used by init and receive contexts for testing. This type should not be used directly, but rather through its HasPolicy interface.
  • TestStateApiDeprecated
    A state api used for testing. Implements HasStateApi.
  • TestStateEntryDeprecated
    A state entry used for testing. Implements HasStateEntry.

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

Type Aliases§