Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update testing documentation #119

Merged
merged 6 commits into from
Sep 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 18 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,37 +225,33 @@ So all Molecule APIs require you to specify your preferred clock behavior:

### Testing

There are two options for testing:

1. Use `moleculeFlow(clock = Immediate)` and test using [Turbine](https://github.com/cashapp/turbine/).
Your `moleculeFlow` will run just like any other flow does in Turbine.

2. Use the `molecule-testing` dependency.
`molecule-testing` provides a `testMolecule` function with a Turbine-like API.
Use `moleculeFlow(clock = Immediate)` and test using [Turbine](https://github.com/cashapp/turbine/). Your `moleculeFlow` will run just like any other flow does in Turbine.

```kotlin
dependencies {
testImplementation("app.cash.molecule:molecule-testing")
// or androidTestImplementation…
}
```

Unlike `moleculeFlow(clock = Immediate)`, `testMolecule` simulates clock ticks:
Calling `awaitItem` yields to running coroutines and "ticks" the Molecule's clock until a change to snapshot state occurs, triggering a fresh recomposition and a new item.
If snapshot state is never changed, no recomposition will ever occur and `awaitItem` will fail.

```kotlin
@Test fun counting() {
testMolecule({ Counter(1, 3) }) {
@Test fun counter() = runTest {
moleculeFlow(RecompositionClock.Immediate) {
Counter()
}.test {
assertEquals(0, awaitItem())
assertEquals(1, awaitItem())
assertEquals(2, awaitItem())
assertEquals(3, awaitItem())
cancel()
}
}
```

For more information see [the documentation](https://cashapp.github.io/molecule/docs/latest/molecule-testing/app.cash.molecule.testing/test-molecule.html).

If you're unit testing Molecule on the JVM in an Android module, please set below in your project's AGP config.

```gradle
android {
...
testOptions {
unitTests.returnDefaultValues = true
}
...
}
```

## License

Expand Down