Skip to main content

Implement basic tests for a contract

A contract's test functions can be used as a simple way to ensure a contract's functions behave as expected. The increment example contract has a function that increments a counter by one on every invocation. The corresponding test invokes that function several time, ensuring with assert_eq!(...) the count increases as expected.

#![cfg(test)]

use super::{IncrementContract, IncrementContractClient};
use soroban_sdk::{Env};

#[test]
fn test() {
// Almost every test will begin this same way. A default Soroban environment
// is created and the contract (along with its client) is registered in it.
let env = Env::default();
let contract_id = env.register_contract(None, IncrementContract);
let client = IncrementContractClient::new(&env, &contract_id);

assert_eq!(client.increment(), 1);
assert_eq!(client.increment(), 2);
assert_eq!(client.increment(), 3);
}