Skip to content

Commit

Permalink
tweaqks
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Dec 2, 2024
1 parent cad3b2a commit d3ff160
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions docs/build/guides/testing/mocking.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ description: Mocking dependency contracts in tests.
sidebar_position: 2
---

Mocks are used in tests to exclude functionality that the test doesn't want to test. Mocks are often used when it's difficult to test against a real thing, such as an API. In Stellar contract tests mocks can be used sparingly because integration testing, that is testing with other contracts, is rather easy. See [Integration Testing] for more details.
Mocks are used in tests to exclude functionality that the test doesn't want to test. Mocks are often used when it's difficult to test against an external component.

:::tip

The Rust Soroban SDK makes it just as easy to test against a real contract as it does to test against a mock of a contract, with little performance impact. In some ecosystems integration tests are avoided for practical reasons. Not in the Stellar ecosystem. Use mocks and integration tests to test thoroughly. 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 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 sets up the `increment-with-pause` contract, as well as a mock pause contract, and invokes the increment contract's function several times under the different conditions the pause contract is expected to be in.
The following tests sets 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.

Expand Down Expand Up @@ -88,7 +94,7 @@ fn test_paused() {
}
```

Most tests, even integration tests, will look very similar to this unit test. They'll do four things:
Most tests, whether you're writing unit, mocks, or integration tests, will look very similar to these tests. They'll do four things:
1. Create an environment, the `Env`.
2. Register the contract(s) to be tested.
3. Invoke functions using the generated client.
Expand All @@ -103,18 +109,13 @@ impl Pause {
fn paused(env: Env) -> ? { ? }
}
```
This is one reason why it's helpful to test and fuzz using real dependencies and to code defensively assuming any external contract call could cause your contract to fail. See [Integration Tests] for how to test with real contracts.

The Rust Soroban SDK handles outgoing contract calls in a way that any unexpected error and unexpected type returned from the called contract to cause execution to stop. See [Making cross-contract calls] for more details.

:::

:::tip
This is one reason why it's helpful to test and fuzz using real dependencies and to code defensively assuming any external contract call could cause your contract to fail. See [Integration Tests] for how to test with real contracts.

The Rust Soroban SDK makes it just as easy to test against a real contract as it does to test against a mock of a contract, with little performance impact. In some ecosystems integration tests are avoided for practical reasons. Not in the Stellar ecosystem. Use mocks and integration tests to test thoroughly. See [Integration Tests].
The Rust Soroban SDK handles contract calls defensively so that any unexpected error and unexpected type returned from the called contract will cause execution to stop. The SDK also provides methods to make these calls and to intercept error situations. See [making cross-contract calls] for more details.

:::

[increment-with-pause contract]: https://github.com/stellar/soroban-examples/blob/main/increment-with-pause/src/lib.rs
[Integration Tests]: ./integration-tests
[Making cross-contract calls]: ../conventions/cross-contract
[making cross-contract calls]: ../conventions/cross-contract

0 comments on commit d3ff160

Please sign in to comment.