How to Write Tests with Mainnet Data
The following is an example of a test that includes a dependency contract into the test, rather than mock it. The test is written to test the increment-with-pause contract and the pause contract. The contract has anincrement function that increases a counter value by one on every invocation. The contract depends on the pause contract to control whether the increment functionality is paused.
The following tests set up the increment-with-pause contract, using a ledger snapshot that has the dependency pause contract already deployed along with its contract data.
-
Use [stellar snapshot create] command to create a snapshot of the pause contract and its data.
The
--ledger <ledger-seq>option can be added to specify a ledger to snapshot. When not specified the latest ledger that’s been pushed to archives is snapshot, that is usually less than 5 minutes old. -
Use
Env::from_ledger_snapshot_file(...)to load the snapshot into anEnvin the test. -
Write a test similar to the following that uses the
Envpreloaded with the contracts and contract data insnapshot.json. The test makes assertions about the behavior of the main contract in the context of the real dependencies and data that the dependencies have on the network.
- Create an environment, the
Env, either withEnv::default()orEnv::from_ledger_snapshot_file(...). - Register the contract(s) to be tested.
- Invoke functions using the generated client.
- Assert the outcome.