Test Events
Tests can assert on events that are expected to be published.
The following example sets up a test environment, registers an increment contract, and checks after the increment invocations which events were published.
#[test]
fn test() {
let env = Env::default();
let contract_id = env.register(IncrementContract, ());
let client = IncrementContractClient::new(&env, &contract_id);
assert_eq!(client.increment(), 1);
assert_eq!(client.increment(), 2);
assert_eq!(client.increment(), 3);
assert_eq!(
// Get all events published since the Env was created.
env.events().all(),
// Compare the events with the expected events.
vec![
&env,
(
contract_id.clone(),
(symbol_short!("COUNTER"), symbol_short!("increment")).into_val(&env),
1u32.into_val(&env)
),
(
contract_id.clone(),
(symbol_short!("COUNTER"), symbol_short!("increment")).into_val(&env),
2u32.into_val(&env)
),
(
contract_id,
(symbol_short!("COUNTER"), symbol_short!("increment")).into_val(&env),
3u32.into_val(&env)
),
]
);
}
For the full example the above snippet is extracted from, see the events example contract.
Guides in this category:
Unit Tests
Unit tests are small tests that test smart contracts.
Mocking
Mocking dependency contracts in tests.
Test Authorization
Write tests that test contract authorization.
Test Events
Write tests that test contract events.
Integration Tests
Integration testing uses dependency contracts instead of mocks.
Fork Testing
Integration testing using mainnet data.
Fuzzing
Fuzzing and property testing to find unexpected behavior.
Differential Tests
Differential testing detects unintended changes.
Differential Tests with Test Snapshots
Differential testing using automatic test snapshots.
Mutation Testing
Mutation testing finds code not tested.
Code Coverage
Code coverage tools find code not tested.
Testing with Ledger Snapshot
Use ledger snapshots to test contracts with ledger data