pub struct MockContract { /* private fields */ }Expand description
Handle to a mock contract registered in a TestEnvironment: a
contract whose entrypoints are native closures instead of rv32 code.
Implementations§
Source§impl MockContract
impl MockContract
Sourcepub fn as_contract(&self) -> Contract
pub fn as_contract(&self) -> Contract
The Contract::kMockTest value identifying this mock.
Sourcepub fn as_hash(&self) -> &SHA256Hash
pub fn as_hash(&self) -> &SHA256Hash
The contract hash (sha256(Contract::kMockTest(index))).
Sourcepub fn deploy_contract_to(&self, addr: &ContractAddress)
pub fn deploy_contract_to(&self, addr: &ContractAddress)
Deploy this prepared mock contract at a fixed address in test setup.
This uses the test environment’s setup path, so it should be called before executing the transaction under test.
Sourcepub fn register_method<F>(&mut self, id: ContractEntrypointID, func: F)
pub fn register_method<F>(&mut self, id: ContractEntrypointID, func: F)
Register func as the entrypoint for method id. The closure runs
inside the frame’s catch site, so panics and aborts out of it map to
frame outcomes.
Panics if id is already registered on this mock: two methods
resolving to one dispatch ID means the second registration would
silently shadow the first, so duplicate definitions (same interface
name, major version, and method name) fail loudly at test setup
instead. There is deliberately no replace/override variant; on-chain
dispatch has no override either.
Send + Sync on func is what keeps a mock body safe to run on the
thread the coroutine resumed on rather than the one that registered it;
see invoke_wrapper. Capturing Rc/RefCell state to record calls
is rejected here – use Arc/Mutex, or an AtomicUsize for a plain
counter.
Sourcepub fn register_unknown_method<F>(&mut self, func: F)
pub fn register_unknown_method<F>(&mut self, func: F)
Install the handler for method ids this contract does not define, replacing any previous one.
The registered closure runs inside the same invoke_wrapper catch
site as a real method, so aborting out of it (e.g. via
thruput_sdk::exn::library_throw) reaches the caller as an ordinary
exception rather than escaping the frame.
Sourcepub fn register<F>(&mut self, f: F) -> &mut MockContractwhere
F: FnOnce(&mut MockContract),
pub fn register<F>(&mut self, f: F) -> &mut MockContractwhere
F: FnOnce(&mut MockContract),
Apply a registrar closure (e.g. a generated __mock function),
for chaining.