Skip to content

Commit f39688d

Browse files
author
Peter Bryant
committed
📝 Add documentation for the test package
1 parent c07fb21 commit f39688d

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Core Concepts
66
* [Core](core-concepts.md)
77
* [Bloc + Compose](bloc-compose.md)
8+
* [Test](bloc-test.md)
89
* [Naming Conventions](naming-conventions.md)
910
* Tutorials
1011
* [Counter](tutorials/counter.md)

docs/bloc-test.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Bloc Test
2+
3+
The `test` package includes some utilities to help you test and mock Blocs and Cubits.
4+
5+
## `testBloc`
6+
7+
The `testBloc` method provides some syntactic sugar, allowing you to write readable tests for your Blocs and Cubits.
8+
9+
```kotlin
10+
class CounterBlocTest {
11+
@Test
12+
fun counterBlocEmitsCorrectStates() = runBlocking {
13+
testBloc(
14+
// The build function should return an instance of the bloc you want to test
15+
build = { CounterBloc() },
16+
// Use the act function to perform operations on your bloc (usually adding events)
17+
act = {
18+
add(Incremented)
19+
add(Incremented)
20+
add(Decremented)
21+
},
22+
// The expect parameter should be a list of functions. Each function takes the next state
23+
// emitted and should return a boolean indicating whether that state is correct.
24+
expect = listOf(
25+
{ equals(1) },
26+
{ equals(2) },
27+
{ equals(1) },
28+
),
29+
)
30+
}
31+
}
32+
```
33+
34+
As well as the parameters shown above, `blocTest` also provides the following options:
35+
36+
* `setUp`: A function executed before `build` which can be used to set up dependencies or do any other initialization
37+
* `tearDown`: A function executed as the final step of the test, which can be used to perform cleanup
38+
* `skip`: An optional `Int` which indicates the number of states to ignore before beginning to make assertions
39+
* `verify`: A function executed after the assertion step, which can be used to perform additional checks and verification
40+
41+
## `mockBloc`, `mockCubit` and `whenListen`
42+
43+
You can use the `mockBloc` and `mockCubit` functions to create mock versions of your Blocs and Cubits. Under-the-hood, these methods use the popular [mockk](https://mockk.io) framework.
44+
45+
Mocking is useful if you want to test a class or function which depends on a Bloc or Cubit. With a mocked version, you can simulate the emission of a particular state or set of states.
46+
47+
The `whenListen` method is provided as a simple way of stubbing the state flow emitted by the mocked Bloc or Cubit.
48+
49+
```kotlin
50+
class MyComposableTest {
51+
@Test
52+
fun testWithMockCubit() {
53+
val cubit = mockCubit<CounterCubit, Int>() // Here, Int represents the state type
54+
55+
whenListen(cubit, flowOf(1,2,3)) // This line sets up the cubit to emit the numbers 1, 2, and 3 in that order.
56+
57+
// Do some testing here...
58+
}
59+
60+
@Test
61+
fun testWithMockBloc() {
62+
val bloc = mockBloc<CounterBloc, CounterEvent, Int>()
63+
64+
whenListen(bloc, flowOf(1,2,3), initialState = 0) // You can pass an optional initial state.
65+
66+
// Do some testing...
67+
}
68+
}
69+
```
70+

docs/getting-started.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ allprojects {
2525

2626
Then add the dependency to your module-level `build.gradle` file.
2727

28-
!> Note: you only need one of the two available dependencies. The `compose` library includes the `core` library.
29-
3028
```groovy
3129
dependencies {
3230
// ...
3331
3432
// Choose EITHER:
35-
implementation 'com.github.ptrbrynt.KotlinBloc:compose:1.0' // For Jetpack Compose apps
36-
implementation 'com.github.ptrbrynt.KotlinBloc:core:1.0' // The pure Kotlin library, for other stuff
33+
implementation 'com.github.ptrbrynt.KotlinBloc:compose:1.1.0' // For Jetpack Compose apps
34+
implementation 'com.github.ptrbrynt.KotlinBloc:core:1.1.0' // The pure Kotlin library, for other stuff
35+
36+
// Optional test helpers:
37+
testImplementation 'com.github.ptrbrynt.KotlinBloc:test:1.1.0'
3738
}
3839
```

0 commit comments

Comments
 (0)