pub type TestInitContext<'a> = TestContext<'a, TestInitOnlyData>;
Expand description

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!

Setters

Every field has a setter function prefixed with set_.

Example

Creating an empty context and setting the init_origin.

let mut ctx = TestInitContext::empty();
ctx.set_init_origin(AccountAddress([0u8; 32]));

Set chain meta data

Chain meta data is set using setters on the context or by setters on a mutable reference of TestChainMeta.

Example

Creating an empty context and setting the slot_time metadata.

let mut ctx = TestInitContext::empty();
ctx.set_metadata_slot_time(Timestamp::from_timestamp_millis(1609459200));

or

let mut ctx = TestInitContext::empty();
ctx.metadata_mut().set_slot_time(Timestamp::from_timestamp_millis(1609459200));

Use case example

type ParameterType = u64;
#[init(contract = "noop", parameter = "ParameterType")]
fn contract_init<S: HasStateApi>(
    ctx: &impl HasInitContext,
    state_builder: &mut StateBuilder<S>,
) -> InitResult<()> {
    let init_origin = ctx.init_origin();
    let parameter: ParameterType = ctx.parameter_cursor().get()?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use concordium_std::test_infrastructure::*;
    #[test]
    fn test() {
        let ctx = TestInitContext::empty();
        let mut state_builder = TestStateBuilder::new();
        ctx.set_init_origin(AccountAddress([0u8; 32]));
        let result = contract_init(&ctx, &mut state_builder);
        // Reads the init_origin without any problems.
        // But then fails because the parameter is not set.
    }
}

Implementations

Create an TestInitContext where every field is unset, and getting any of the fields will result in fail!.

Set init_origin in the TestInitContext

Trait Implementations

Data needed to open the context.

Open the init context for reading and accessing values.

Who invoked this init call.