cosmwasm_std::testing

Function mock_env

Source
pub fn mock_env() -> Env
Expand description

Returns a default environment with height, time, chain_id, and contract address. You can submit as is to most contracts, or modify height/time if you want to test for expiration.

This is intended for use in test code only.

The contract address uses the same bech32 prefix as MockApi. While this is good for the majority of users, you might need to create your Envs differently if you need a valid address using a different prefix.

ยงExamples

Create an env:

use cosmwasm_std::testing::mock_env;

let env = mock_env();
assert_eq!(env, Env {
    block: BlockInfo {
        height: 12_345,
        time: Timestamp::from_nanos(1_571_797_419_879_305_533),
        chain_id: "cosmos-testnet-14002".to_string(),
    },
    transaction: Some(TransactionInfo { index: 3 }),
    contract: ContractInfo {
        address: Addr::unchecked("cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs"),
    },
});

Mutate and reuse environment:

use cosmwasm_std::testing::mock_env;

let env1 = mock_env();

// First test with `env1`

let mut env2 = env1.clone();
env2.block.height += 1;
env2.block.time = env1.block.time.plus_seconds(6);

// `env2` is one block and 6 seconds later

let mut env3 = env2.clone();
env3.block.height += 1;
env3.block.time = env2.block.time.plus_nanos(5_500_000_000);

// `env3` is one block and 5.5 seconds later