|
| 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 | + |
0 commit comments