Type Definition concordium_std::test_infrastructure::InitContextTest [−][src]
pub type InitContextTest<'a> = ContextTest<'a, InitOnlyDataTest>;
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 = InitContextTest::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 ChainMetaTest
.
Example
Creating an empty context and setting the slot_time
metadata.
let mut ctx = InitContextTest::empty();
ctx.set_metadata_slot_time(1609459200);
or
let mut ctx = InitContextTest::empty();
ctx.metadata_mut().set_slot_time(1609459200);
Use case example
#[init(contract = "noop")]
fn contract_init<I: HasInitContext, L: HasLogger>(
ctx: &I,
_amount: Amount,
_logger: &mut L,
) -> InitResult<()> {
let init_origin = ctx.init_origin();
let parameter: SomeParameterType = ctx.parameter_cursor().get()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use concordium_sc_base::test_infrastructure::*;
#[test]
fn test() {
let mut ctx = InitContextTest::empty();
ctx.set_init_origin(AccountAddress([0u8; 32]));
...
let result = contract_init(&ctx, 0, &mut logger);
// Reads the init_origin without any problems.
// But then fails because the parameter is not set.
}
}
Implementations
Create an InitContextTest
where every field is unset, and getting any
of the fields will result in fail!
.
Set init_origin
in the InitContextTest
Trait Implementations
Who invoked this init call.