Mocking
Mocks are used in tests to exclude functionality that a test doesn't want to test. Mocks are used when it's difficult to test against an external component.
The Soroban Rust SDK makes it just as easy to test against a real contract as it does to test against a mock of a contract. In some ecosystems integration tests are avoided. Not in the Stellar ecosystem. See Integration Tests.
How to Write Tests with Mocks
The following is an example of a test that uses a mock, written to test the increment-with-pause contract. The contract has an increment function that increases a counter value by one on every invocation. The contract depends on another contract that controls whether the increment functionality is paused.
The following tests set up the increment-with-pause contract, as well as a mock pause contract, and invokes the increment contract's function several times under different conditions the pause contract is expected to be in.
The following test checks that when the pause contract is not paused, the increment contract functions.
#![cfg(test)]
use crate::{Error, IncrementContract, IncrementContractArgs, IncrementContractClient, Pause};
use soroban_sdk::{contract, contractimpl, Env};
mod notpaused {
use super::*;
#[contract]
pub struct Mock;
#[contractimpl]
impl Pause for Mock {
fn paused(_env: Env) -> bool {
false
}
}
}
#[test]
fn test_notpaused() {
let env = Env::default();
let pause_id = env.register(notpaused::Mock, ());
let contract_id = env